A message body writer for Java type, class myPackage.B, and MIME media type, application/octet-stream, was not found

后端 未结 10 1346

I am new at RESTful webservices and was trying to update my @OneToMany relationship from a standalone client application, but I am not able to do that. I am usi

相关标签:
10条回答
  • 2020-11-29 06:53

    This solved my issue.

    http://www.markhneedham.com/blog/2012/11/28/jersey-com-sun-jersey-api-client-clienthandlerexception-a-message-body-reader-for-java-class-and-mime-media-type-applicationjson-was-not-found/

    Including following dependencies in your POM.xml and run Maven -> Update also fixed my issue.

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.19.1</version>
    </dependency>
    <dependency>
       <groupId>com.owlike</groupId>
       <artifactId>genson</artifactId>
       <version>0.99</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-29 06:58

    i was facing the same problem for a get method i was returning an "int" for the @get method Strangely when i change the return type to String the error was gone.Give it a try and if someone knows the logic behind it kindly share it

    0 讨论(0)
  • 2020-11-29 07:02

    In your client code you are not specifying the content type of the data you are sending - so Jersey is not able to locate the right MessageBodyWritter to serialize the b1 object.

    Modify the last line of your main method as follows:

    ClientResponse response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1);
    

    And add @XmlRootElement annotation to class B on both the server as well as the client sides.

    0 讨论(0)
  • 2020-11-29 07:02

    Adding reference to:

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey1.version}</version>
    </dependency>
    

    As long as adding clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true); on client creation solved the issue for me:

    ClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    Client client = Client.create(clientConfig);
    
    0 讨论(0)
  • 2020-11-29 07:05

    This also happens if you're missing an empty public constructor for the Entity (could be for JSON, XML etc)..

    0 讨论(0)
  • 2020-11-29 07:06

    You have to do two things to remove this error.

    1. The @xmlElement mapping in the model
    2. The client side:

      response = resource.type(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //consume

      or

      response = resource.accept(MediaType.APPLICATION_XML).put(ClientResponse.class, b1); //produce

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