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

57,120

Solution 1

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.

Solution 2

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>

Solution 3

Adding below dependency solved my issue. As per @eugen I tried adding Genson dependency which helped get rid of the exception, however my server was throwing 400 bad request for some reason. It seems like Genson was not able to stream the json request properly. But below dependency worked fine. Hope it helps others as this problem was driving me nuts!

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.3.0</version>
</dependency>

Solution 4

Moving jersey-json dependency to the top of the pom.xml solved this problem for me.

<dependencies>
  <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.18.1</version>
  </dependency>

  <!-- other dependencies -->

</dependencies>

Solution 5

Check under Maven Dependencies in the Package Explorer, if you don't see that or jersey-bundle-1.8.jar, then you probably have specified just jersey-server. Change the dependency in pom.xml to jersey-bundle and it should work.

Share:
57,120
dhartwich
Author by

dhartwich

Updated on November 28, 2020

Comments

  • dhartwich
    dhartwich over 3 years

    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;
    
    
        }
    }