I'm trying to implement a Resource with the jersey framework. But if I call my resource, I will get a parsing exception, that the parser can't parse JSONArray datatype.
Here's my resource structure:
@Path("/books")
@Consumes("application/json")
public class BookResource {
@GET
@Produces("application/json")
public JSONArray getAllBooksOfCurrentUser() {
Book book = new Book();
book.initDummyBook();
JSONArray books = new JSONArray();
Page page = new Page();
page.setBook(book);
page.setNumber(22);
ReadStatistic readStatistic = new ReadStatistic();
readStatistic.setLastReadAt(new Date());
readStatistic.setLastPageRead(page);
books.put(book.simpleRepresantationWithLastReadAsJson(readStatistic));
return books;
}
}
And this is the exception:
29.10.2010 11:42:57 org.apache.catalina.core.StandardWrapperValve invoke SCHWERWIEGEND: Servlet.service() for servlet ServletAdaptor threw exception org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.codehaus.jettison.json.JSONArray and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) at org.codehaus.jackson.map.ser.StdSerializerProvider$1.serialize(StdSerializerProvider.java:62) at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:296) at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:224) at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:925) at org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:497) at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:299) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1326) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1239) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:497) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:684) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174) at org.apache.coyote.http开发者_StackOverflow中文版11.Http11Processor.process(Http11Processor.java:879) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) at java.lang.Thread.run(Thread.java:680)
Does anyone knows whats going wrong?
I'm new to Jersey, but I think if your method returns anything but a String, there needs to be a way for JAXB to serialize it. I just had my method return a String, changed my return statement to:
return myJsonArray.toString();
and continued to use the
@Produces("application/json")
This seemed to work. There's probably a serializer in the codehaus jar, but haven't dived into looking for that.
its been a while since this question was posed but I just ran across the same problem. I looked over some Jersey examples and found differences in the deployment descriptors' web.xml files. This is what worked for me:
<servlet-class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.example.your.resources</param-value>
</init-param>
No idea if this is a bug or even what the differences are between the different Jersey servlet-classes. Just wanted to place the (some?) answer here for the record.
And also just for the record: In this case I used Jersey 1.5.
UPDATED: Just tested this on Jersey 1.11, and it works as well.
I had this same problem when using the different jars for jackson: jackson-xc-1.9.2.jar: jackson-mapper-asl-1.9.2.jar: jackson-jaxrs-1.9.2.jar: jackson-core-asl-1.9.2.jar
Not sure what is different but when I switched to jackson-all-1.9.2.jar, the exception stopped.
JAXB cant serialize JSONArray and JSONObject by default.
Transform your JSONArray to a String is an option.
There is another way that works fine to me:
- Change the JSONArray to a List type;
- Change your array objects to a Map type;
JAXB serialize Lists and Maps by default, this solution works fine to me.
精彩评论