How to produce JSON output using Jersey 1.6 using JAXB

@XmlRootElement public class Todo { private String s = "test"; public String getS() { return s; } public void setS(String s) { this.s = s; } } 

and service:

 @Path("/test") public class Service { @GET @Produces({MediaType.APPLICATION_JSON }) public List<Todo> getAllGadgets() { return Arrays.asList(new Todo[] { new Todo() }); } } 

my web.xml:

 <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.test</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> 

All this work, if I set MediaType.APPLICATION_XML for Produces annotation. But for JSON, I get the following exception:

SEVERE: Fixed exception for answer: 500 - internal server error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A post author for the Java class java.util.Arrays $ ArrayList and Java type java. util.List and application like MIME media / json not found

I am using Jersey 1.6 and according to the tutorial, the JSON format should work with JAXB without any additional programming. What's wrong?

+27
java json rest jackson jax-rs
May 17 '11 at 6:31 am
source share
6 answers

I solved it. All I had to do was add the jersey-json-1.6.jar library to the project (this is not a required part of the jersey).

+32
May 17 '11 at 6:40 a.m.
source share

Add the following parameter to the Jersey server in the web.xml file, this is required for the latest versions of the 1.x hard servlet.

  <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 
+14
Aug 13 2018-12-12T00:
source share

Other answers did not work for me, but I finally got it for working with JSON.

I used jersey-bundle-1.17.jar (also tried with asm-3.1.jar and jersey-json-1.17.jar added to classpath and still didn't work). I finally tried downloading zip, which includes 12 different cans. As soon as I added all 12 cans to my class path, I finally got rid of the error and works fine with JSON.

Hope this helps someone.

Update: Here is the link to the zip file containing 12 jar files: jersey-archive-1.17.zip

Another update for Maven users: Add the following to your pom.xml to get 12 cans individually:

  <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.17.1</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-xc</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.1</version> </dependency> 
+6
Mar 27 '13 at 15:27
source share

I use the Google App Engine and struggle with this a lot if you use jersey-bundle-1.17.jar most of the work until you add

 <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 

You will receive many strange messages. This is because you miss a few cans of Jackson. If you go to the Jersey homepage and download the zip and kit. Just drop the package and from zip you need to add 4 Jackson jars to your class path and you should get everything that works without errors.

Adding jackson-jaxrs-1.9.2.jar resolves this error below

 SEVERE: The registered message body writers compatible with the MIME media type are: application/json -> 

Adding jackson-xc-1.9.2.jar allows this attack below

java.lang.NoClassDefFoundError: org / Codehaus / Jackson / xs / JaxbAnnotationIntrospector

Hope this helps someone.

+6
May 10 '13 at 19:28
source share

The exclusive message about the message author specified in the OP will be raised unless you annotate the POJO (or base POJO) using @XmlRootElement .

For example:

 @XmlRootElement public class BaseBean { private Boolean success = Boolean.TRUE; private String message; /** * Empty constructor to satisfy requirements of JAXRS. */ public BaseBean() {} /** * Returns a simple message to accompany the success/failure. * @return */ public String getMessage() { return message; } /** * Sets the message (if required). * @param message */ public void setMessage(String message) { this.message = message; } /** * Returns a flag indicating whether a request for content was * successful. * @return */ public Boolean getSuccess() { return success; } /** * Marks the success of a request for content. * @param success */ public void setSuccess(Boolean success) { this.success = success; } } 
0
May 19 '15 at 1:13
source share

Kamran's answer worked for me, just to extend XML more:

  <servlet> <servlet-name>JerseyServletContainer</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> 
0
Dec 15 '15 at 18:48
source share



All Articles