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

有些话、适合烂在心里 提交于 2019-11-28 23:05:18
eugen

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.

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>

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>

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>

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.

Jersey 2.4: I faced this problem too and posting this incase it helps someone. I picked this up from the Jersey API Specification for JSON Integration : (https://jersey.java.net/documentation/latest/user-guide.html#json)

I used Jettison to integration with Jersey with JSON. But even after adding all the libraries I got the error: A message body writer for Java Class and MIME mediatype application/json was not found

Its all about adding the right libraries and also registering features> I added one extra library in addition to all the others that Jersey provides: jersey-media-json-jettison-2.4.jar. And added the below code to register Jettison Feature.

   public class MyApplication extends ResourceConfig{
      public MyApplication() {
          register(JettisonFeature.class);
          packages("org.XXX.XXX.XXX");   
    }
MankitaP

Remove @Produces(MediaType.APPLICATION_JSON); and use JSONLibraries.zip file to convert your data to and from JSON. It is working perfectly.The problem with above code is that it is using maven and and jackson combination. So APPLICATION_JSON cannot be found when you are using only jersey. I worked with to/from JSON conversion stuff, It gave me same error, I tried with this solution and it was working properly.

Code:

@Path("/hellojson")
public class JSONService {

    @GET
    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 JSONObject.fromObject(availRoutes.toString()));
    }
}

This question was asked a while ago, but for those reading, note that jersey servlet container has it's own POJO mapper.

Enable it by adding the following to your web.xml

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

I did notice some discrepancies in how jersey and genson map objects. For example, if a class has a field of type A which is set to an instance of B, which extends A, genson will not properly serialize B, while jersey does.

The above issue will be solved by using the jackson library instead of the genson.

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.25</version>
    </dependency>

As in my case genson was giving an empty json in the response but will be fine if we are converting the json to string.But will create an undesirable output because in the response you will get a string not a json.So better to go with the jackson library which will do it very easily.

Note: Jackson will convert your response to the json only if the class is implementing the Serializable Interface.So if you are sending JSONObject, then better to send the List of JSONObject.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!