I'm trying to return a List from jersey, which works fine in XML, but when I go to output it as JSON, it claims, "A messag开发者_如何学运维e body writer for Java class ... and Java type ... and MIME media type application/json was not found".
I have not done any configuration for dealing with utility lists, as i thought that jersey + jersey-json-1.9 handled this stuff automagically for JSON the same way it has for XML.
Anyone else have any luck with this?
I found that my first stab at this same problem failed with this error message, and the solution was as given in another SO question, Jersey: com.sun.jersey.server.impl.template.ViewableMessageBodyWriter: I had forgotten to add the jersey-json module to my project.
You should not need any wrappers for lists with JSON, but you do need to enable "POJO mapping" style of JSON support.
Answered. This required the creation of a provider. Code given below:
@Provider
@Singleton
@Produces(MediaType.APPLICATION_JSON)
public class ContextResolver extends JacksonJaxbJsonProvider{
public ContextResolver() throws Exception {
super();
ObjectMapper mapper = new ObjectMapper();
setMapper(mapper);
}
}
I managed to solve JSON array "bug" in recent Jersey json library (v1.14 Sep 2012). Secret ingredient is JSONConfiguration and ContextResolver magic. See my following post it has a full code example, customized ContextResolver and rest Application class might be somewhat fuzzy logic in first look.
How to serialize Java primitives using Jersey REST
Primitives and zero or single-element List array are properly serialized to JSON document. Without customized resolver you don't get proper json[] array fields if Java list is empty. My post lists all the .jar libraries you need. I am using the most recent v1.14 Jersey archive.
精彩评论