Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey: A message body writer for Java Class and MIME mediatype application/json was not found

Tags:

after trying to figure out what's my problem I finally decided to ask you how to solve my problem. I've seen different people with the same problem and I tried all the things they were adviced to do but nothing helped with my issue. So basically I'm having a RESTful Service which I build using Jersey. For my client I would like to return an object in JSON Format. I read through different tutorials and decided that it makes sense to use jersey-json-1.8 library. I added everything to my project as usual and tried to run it but each time I'm calling the service (via get request atm.) I'm getting HTTP Error Code 500 (internal server error) and my server responds that no message body writer could be found. If I'm returning XML it works just fine and everything is great. I also tried copying jersey-json-1.8.jar to my Tomcat lib folder because I had to do this with the mysql lib I'm using but it didn't help either. I would be really glad if you could help me out to get this stuff working! If you need any more information just leave a comment and I'll provide it as quickly as humanly possibly :)

My project setup is: 3 Different packages 1. My RESTfulServices 2. My Java Work where i handle SQL connections etc. 3. A package where I store all my models that I need to work with and that I want to return in JSON Format (in my example a route for a testdrive)

A Tomcat Webserver IDE: Eclipse I'm using Maven

No matter what or how I'm trying to return the Object it just won't work and I'm constantly getting the error message :

Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.mykong.models.Teststrecke, and Java type class com.mykong.models.Teststrecke, and MIME media type application/json was not found 

EDIT: Here's my JSON Service Method

@Path("/hellojson") public class JSONService {       @GET     @Produces(MediaType.APPLICATION_JSON)     public ArrayList<Route> getJSONMsg()       {          Route ts = new Route();         ts.setId(1);         ts.setName("HelloWorld");           Route ts2 = new Route();         ts2.setId(2);         ts2.setName("HelloWorld");           ArrayList<Route> availRoutes = new ArrayList<Route>();         availRoutes.add(ts);         availRoutes.add(ts2);           return availRoutes;       } } 
like image 693
dhartwich Avatar asked Nov 18 '12 10:11

dhartwich


2 Answers

Try adding Genson library to your classpath http://owlike.github.io/genson/. It is a json<>java streaming and databinding api. It integrates well with jersey, to get you running you need 0 configuration. Jersey detects that the library is in your classpath and enables json mapping by delegating to Genson.

like image 119
eugen Avatar answered Oct 12 '22 15:10

eugen


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> 
like image 44
borchvm Avatar answered Oct 12 '22 13:10

borchvm