I am trying with simple Jersey + JSON example but I get following error
message body writer for Java class com.test.jsonexample and MIME media type application/json w
First of all if your @POST
web-service returns a response you must add @Produces
annotation.
Ensure that jersey-json
lib is in your classpath.
Try removing the toString()
method because it may broke the beans structure format.
Add the following dependency to solve the problem
"javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.test.Jsonexample, and Java type class com.test.Jsonexample, and MIME media type application/json was not found
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)"
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18</version>
</dependency>
This problem is fixed with jersey-bundle-1.8.jar
When using jersey-bundle-1.19.1.jar (non-maven) I had to remove this:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
from web.xml file.
I know that documentation suggests this section is necessary but surprisingly it was a reason of the problem.
Fixed issue by adding the following to my pom.xml:
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
Documentation can be found at: https://code.google.com/p/genson/
For new version of Maven, if you have dependency problems, here is a good official reference: https://jersey.java.net/documentation/latest/modules-and-dependencies.html#d0e383 (Section 2.3.1, 2.3.2)
And also here is a good tutorial for installing maven and tuto of Jersey in eclipse: http://javaposts.wordpress.com/2012/01/14/maven-rest-jersey-eclipse-tutorial/
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- if you are using Jersey client specific features -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>