Jersey + Json media type application/json was not found

前端 未结 15 1013
轻奢々
轻奢々 2020-12-06 01:03

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

相关标签:
15条回答
  • 2020-12-06 01:40

    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.

    0 讨论(0)
  • 2020-12-06 01:40

    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)"
    

    Solution :

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.18</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-06 01:42

    This problem is fixed with jersey-bundle-1.8.jar

    0 讨论(0)
  • 2020-12-06 01:44

    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.

    0 讨论(0)
  • 2020-12-06 01:45

    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/

    0 讨论(0)
  • 2020-12-06 01:47

    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>
    
    0 讨论(0)
提交回复
热议问题