简体   繁体   中英

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)

您需要使用@XmlRootElement注释Region类。

as a quick solution I see:

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

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

@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!

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.

Add dependency

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey-version}</version>
</dependency>
    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();

PS: Don't down-vote the answer if it isn't working for you.

在类Region之前添加@@XmlRootElement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM