I have this simple example to return a list of User in JSON. It works fine with XML, but not working with JSON. Not sure what I'm doing wrong. Is there anything else I need to do? I created the project using jersey-quickstart (maven) and uncommented the dependency to support JSON.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
On my User domain I have the @XmlRootElement annotation and there are just 2 fields. Id (long) and Username (String)
And that's what I have on my resource:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(){
return userService.getAllUsers();
}
And that's what I have on my service:
public List<User> getAllUsers(){
List<User> users = new ArrayList<User>();
User u1 = new User(1l, "user_1");
User u2 = new User(2l, "user_2");
User u3 = new User(3l, "user_3");
users.add(u1);
users.add(u2);
users.add(u3);
return users;
}
By changing the APPLICATION_JSON to APPLICATION_XML it works fine and return the xml with the list of users. With APPLICATION_JSON I get this:
SEVERE: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<com.wordpress.whiteboardcoding.agenda.domain.User>.
Not sure if there is something else I had to do other then uncommenting the jersey-media-json-binding. Any thoughts?
Trying to find out if there is something different as it's not using the jersey-media-moxy anymore.
Thanks!