3

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 message 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?

Rannick
  • 598
  • 5
  • 19

4 Answers4

2

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.

Community
  • 1
  • 1
metamatt
  • 13,809
  • 7
  • 46
  • 56
1

You should not need any wrappers for lists with JSON, but you do need to enable "POJO mapping" style of JSON support.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
0

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.

Community
  • 1
  • 1
Whome
  • 10,181
  • 6
  • 53
  • 65
0

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);
    }
}
Rannick
  • 598
  • 5
  • 19