Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A message body writer for Java class java.util.ArrayList

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) 
like image 502
n4rzul Avatar asked Jun 14 '12 20:06

n4rzul


2 Answers

You need to annotate Region class with @XmlRootElement.

like image 60
Martin Matula Avatar answered Oct 07 '22 00:10

Martin Matula


as a quick solution I see:

final List<MyModel> myList = new ArrayList<>(); Response.ok().entity(new GenericEntity<List<MyModel>>(myList) {}).build(); 
like image 31
Rodislav Moldovan Avatar answered Oct 07 '22 00:10

Rodislav Moldovan