Jersey + Json media type application/json was not found

前端 未结 15 928
轻奢々
轻奢々 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:27

    I tried to download the following jar libraries:

    • jersy-json.jar version 1.19 from MVNrepository,
    • gson version 0.99 from MVNrepository
    • jersey-media-json-jackson-2.15 from MVNrepository
    • jersey-bundle-1.8 from java2s.com

    When I download gson 0.99 my rest service work well.

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

    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'
    )
    
    0 讨论(0)
  • 2020-12-06 01:35

    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.

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

    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>
    
    0 讨论(0)
  • 2020-12-06 01:37

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

    1. 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.

    2. 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.

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

    I had the same problem and I fixed it with removing all json dependencies and adding com.owlike genson 0.99

    0 讨论(0)
提交回复
热议问题