Jersey GenericEntity Not Working

2019-05-07 02:13发布

问题:

I am using Jersey's GenericEntity to return a list as json.

However I'm getting the exception

A message body writer for Java type, class java.util.Arrays$ArrayList, and MIME media type, application/xml, was not found
16-May-2011 11:16:31 com.sun.jersey.spi.container.ContainerResponse traceException
SEVERE: Mapped exception to response: 500 (Internal Server Error)....

I know this means that Jersey is not setup to map to json properly.

Where do I need to give more information to jersey. I am not using Maven.

The code that breaks is

List<String> list = Arrays.asList("test", "as");
return new GenericEntity<List<String>>(list) {};

回答1:

A GenericEntity is meant to be embedded in a Response that way:

public Response get() {
    List<String> list = Arrays.asList("test", "as");
    return Response.ok(new GenericEntity<List<String>>(list) {}).build();
}