Reputation: 4250
I'm trying to return a List in a few different media types. I'm hosting this on glassfish 4.
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
public List<String> getSessions(){
return Arrays.asList("foo","bar");
}
If I do a plain GET without specifying the type, I get a 500 internal server error. Specifying application/json also gives me a 500 internal server error. The server log shows absolutely no errors.
If I do a GET with Accept: text/plain, I get the following:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain, type=class java.util.Arrays$ArrayList, genericType=java.util.List.
I would have thought glassfish could figure out what to do with basic collection classes here. They silently fail when I try to parse them into json and apparently don't have a messagebodywriter to turn them into strings. Am I missing something obvious?
Update I've added Jackson as a jaxrs json provider using the instructions here: https://github.com/FasterXML/jackson-jaxrs-providers
Apparently with the current release, I do not need to specifically register JacksonFeature.class, nor am I able to, because there is no JacksonFeature class, just a JacksonFeatures interface.
I added this dependency to my project:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.0</version>
</dependency>
I am now able to return json if I do so through a Response object as follows:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSessions(){
Response r = Response.ok(Arrays.asList("foo","bar")).build();
return r;
}
However, attempting to return List still fails with a 500 internal server error and no message to the server error log.
I'm willing to use the Response method if this is the correct approach, but it seems like the other way should work.
Upvotes: 3
Views: 10421
Reputation: 121
you can use the "Response" but you have to pass the list as entity so the response builder may formate the response correctly. in your code, you delegate your list the ok() method which doesn't know what type ArrayList means, so in your code simply add the entity like this :
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSessions(){
Response r = Response.ok().entity(Arrays.asList("foo","bar")).build();
return r;
}
NOTE: entity method can map many MediaType, that means you can use this :
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
or even if you would use your own customized MediaType, it will do the job :
@Produces({"application/vnd.myList+json", "application/vnd.myList+xml"})
Upvotes: 0
Reputation: 1005
You have to enable JSON support for Jersey (the JAX-RS implementation in Glassfish). Here you can find the details:
By the way this minimal configuration was enough for me after adding the jersey-media-json-jackson dependency:
@ApplicationPath("/rest")
public class App extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
// enable JSON support through Jackson
classes.add(JacksonFeature.class);
// my rest service
classes.add(MyRESTService.class);
return classes;
}
}
Upvotes: 4