28

I received the below stack trace when accessing on of my jax-rs resources.

I'm using Tomcat 7, with Jersey 1.12 and Hibernate 4 and MySQL.

I found this tutorial while searching for a solution: http://aruld.info/handling-generified-collections-in-jersey-jax-rs/ but none of the examples listed seemed to work.

What am I missing here?

Please no answers that have me writing MessageBodyWriters, this should work out the box. (And I know there's a solution, I just can't figure it out.)

Here are all my jars:

antlr-2.7.7.jar
asm-3.1.jar
commons-collections-3.2.1.jar
dom4j-1.6.1.jar
gson-1.7.1.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.CR2.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
jersey-client-1.12.jar
jersey-core-1.12.jar
jersey-json-1.12.jar
jersey-server-1.12.jar
jersey-servlet-1.12.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
mysql-connector-java-3.1.12-bin.jar

Here is my resource class and method:

@Path("/region")
public class RegionService {
    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.APPLICATION_JSON)   
    public JResponse<List<Region>> region() {
        RegionDao regionDao = new RegionDao();
        regionDao.openSession();
        List<Region> regions = regionDao.getAll();
        regionDao.closeSession();
        return JResponse.ok(regions).build();
    }
}

And here is the stacktrace:

SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<campher.hibernate.entities.Region>, and MIME media type application/json was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1451)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
6
  • 5
    Seems similiar: stackoverflow.com/questions/5161466/… Jun 14, 2012 at 20:43
  • 2
    Great thanks. That's exactly it!! It was actually the question that solved my problem. I needed to add the com.sun.jersey.api.json.POJOMappingFeature parameter. facepalm
    – n4rzul
    Jun 14, 2012 at 20:54
  • Whats the best way to go about this question now? Could you post your comment as an answer or should I just delete this silly question altogether?
    – n4rzul
    Jun 14, 2012 at 20:55
  • 3
    don't delete it. this is a useful google search result. Nov 2, 2012 at 19:56
  • Hi I'm having the same problem but I don't understand what you mean with "add the com.sun.jersey.api.json.POJOMappingFeature parameter". Where / how do you do that?
    – Nilzor
    Jan 30, 2013 at 17:46

8 Answers 8

37

You need to annotate Region class with @XmlRootElement.

5
  • Thank you! I had already spent 5 hours pulling my hair out on this!
    – Joe M
    Sep 27, 2012 at 7:40
  • 1
    I just came here for the second time. You deserve my +1
    – luigi7up
    Feb 21, 2013 at 16:23
  • stackoverflow.com/questions/17342218/… ..can you solve this ?? Jun 27, 2013 at 11:57
  • Would it be possible to get a bit more detail here, and possibly an example of use? I believe I have the same problem, but I don't actually understand the context of what you mean. I'm afraid I don't understand even the basic fundamentals of jersey. Jul 25, 2013 at 14:07
  • If you get the "message body writer was not found for Java class foo", you need to go into foo.java and add @XmlRootElement on the line before the "public class foo" bit.
    – Redboots
    Sep 23, 2015 at 20:09
10

as a quick solution I see:

final List<MyModel> myList = new ArrayList<>();
Response.ok().entity(new GenericEntity<List<MyModel>>(myList) {}).build();
8

The other answer didn't work for me (I already had the XmlRootElement annotation), but I finally got it to work with JSON. It was returning XML just fine, just not JSON.

I was using the jersey-bundle-1.17.jar (also tried with the asm-3.1.jar and jersey-json-1.17.jar added to classpath and still didn't work). I finally just tried downloading the zip that includes 12 different jars. Once I added all 12 jars to my classpath I finally got rid of the error and works great returning JSON.

I hope this helps somebody.

EDIT: Here is a link to the zip file that contains the 12 jar files: jersey-archive-1.17.zip

4
2

@XmlRootElement with this annotation it's possible to select JSON ou XML in Jersey. Thank you very much!

Just add to rest this: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

On the request you set on the header if you won the return in xml or in json!

1

As you are expecting List, you can annotate your bean class with @XmlRootElement. But the same won't work for Map. Jersey internally uses moxy to do the job done.

1

Add dependency

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey-version}</version>
</dependency>
-2

add @@XmlRootElement before your class Region

-2
    You can use JSONObject to create a Json Response 
    
    eg: 
    
    List<Region> regions = regionDao.getAll();
    JSONArray list = new JSONArray();
    for(Region region : regions )
    {
       JSONObject jObject= new JSONObject();
       //put all the data in json object
       jObject.put(region.getSomething());
       // and put this Jsonobject in JsonArray
       list.add(jObject);
    }

Or 

Response.ok().entity(new GenericEntity<List<Region>>(regions) {}).build();
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.