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
I tried to download the following jar libraries:
When I download gson 0.99 my rest service work well.
Tried with 'com.sun.jersey:jersey-bundle:1.19'
and it did not solve.
Setting the dependency on genson
resolved for me.
runtime(
'com.owlike:genson:0.99'
)
Also check the actual type parameter of the post method.
Having also gson as a dependency, i should be using JSONObject (Jettison from Jersey-Json) in stead of JsonObject (gson), which resulted in the same exception.
Include this dependencies in your POM.xml and run Maven -> Update
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
I've spent literally 4 days on this and after googling for hours and hours why my POST wasn't working at all. I was getting this error as well as other errors to do with MOXY such as classdefnotfound error, due to people on stackoverflow suggesting to use MOXY instead of Jackson. I also tried the genson library which provided no difference whatsoever either.
I ended up completely removing MOXY and only keeping Jackson. After hours I finally found the solution, which made me angry a little bit due to how simple it was. My workspace is separated into multiple different projects, one for the webapplication which holds web.xml and another for the RESTful API.
I'm also running Jersey 2, which was another problem as a lot of the accepted answers for this problem only talked about Jersey 1 and it was super confusing when I was first trying to solve this.
After hours and hours of trying it ended up coming down to this (using the structure of code that was posted in OP):
You HAVE to include @XmlRootElement at the top of your POJO class, contrary to many of the examples on stackoverflow which seem to omit it.
You HAVE to include the following in both the project that includes your web.xml as well as the project that includes your REST service and associated classes. Change the version which is relevant to version of Jersey you are using.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5.1</version>
</dependency>
That's it. You don't need to include anything else besides this and the relevant Jersey libraries which you are using.
I seriously hope this helps someone else save hours of time trying to troubleshoot this.
I had the same problem and I fixed it with removing all json dependencies and adding com.owlike genson 0.99