Reputation: 2085
I am trying to create a very simple REST service using Jersey. Here is the service code
@Path("/UserService")
public class UserService {
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() {
User user = new User(1, "Thomas", "Greene");
List<User> userList = new ArrayList<User>();
userList.add(user);
return userList;
}
}
When I run it through Postman, it returns me a XML response
Now, I want to get a JSON response back. So, I changed the mediatype to application/json
:
@Path("/UserService")
public class UserService {
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(){
User user = new User(1, "Thomas", "Greene");
List<User> userList = new ArrayList<User>();
userList.add(user);
return userList;
}
}
It gives me the below error in Tomcat logs:
SEVERE: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List.
Can someone please guide me how to get a JSON response back?
Upvotes: 7
Views: 20329
Reputation: 1
The real problem is that you need to put your List inside a class, try this:
public class UserListClass() {
private List<User> userList = new ArrayList<User>();
public List<User> getUserList() { return userList; }
public setUserList(List<User> userList) { this.userList = userList; }
}
and your code like this:
@Path("/UserService")
public class UserService {
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public UserListClass getUsers() {
User user = new User(1, "Thomas", "Greene");
List<User> userList = new ArrayList<User>();
userList.add(user);
UserListClass ulc = new UserListClass();
ulc.setUserList(userList);
return ulc;
}
}
Upvotes: 0
Reputation: 59
I tried a ton of these dependencies but none of them worked for my version of Jersey3. What I needed to do was turn the Arraylist into an actual array. I managed this with toArray()
and it started serializing correctly!
Upvotes: 1
Reputation: 1216
Your xml was working so I assume that you have @XmlRootElement
annotation in your User
class.
The thing is, it knows how to convert it to xml with the annotation @XmlRootElement
but it doesn't know how to convert it to JSON.
So for making it convert everything to JSON with the same annotation of xml(ie @XmlRootElement
) we can add
jersey-media-moxy-<whatever version>.jar
or for maven users
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
Also it should have a no argument constructor
Upvotes: 2
Reputation: 131117
To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson
module to your pom.xml
file:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.2</version>
</dependency>
And then register the JacksonFeature
in your Application
/ResourceConfig
subclass.
For more details, have a look at Jersey documentation.
Upvotes: 14
Reputation: 635
I am a bit upset about JAXB binding as well at the moment, therefore let me summarize my findings here - please correct me if I say something stupid:
I hope this helps others.
Upvotes: 3
Reputation: 2117
You need a json serializer on your class path to make this work.
Just add jackson and jersey will use this in the writer. E.g. if you are using maven, add this to the pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.4</version>
</dependency>
Upvotes: 2