Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML

前端 未结 13 1775
渐次进展
渐次进展 2020-11-27 04:57

I am actually new to REST WS but really I don\'t get this 415 Unsupported Media Type.

I am testing my REST with Poster on Firefox and the GET

相关标签:
13条回答
  • 2020-11-27 05:38

    Add Content-Type: application/json and Accept: application/json in REST Client header section

    0 讨论(0)
  • 2020-11-27 05:40

    I had this issue and found that the problem was that I had not registered the JacksonFeature class:

    // Create JAX-RS application.
    final Application application = new ResourceConfig()
        ...
        .register(JacksonFeature.class);
    

    Without doing this your application does not know how to convert the JSON to a java object.

    https://jersey.java.net/documentation/latest/media.html#json.jackson

    0 讨论(0)
  • 2020-11-27 05:41

    The issue is in the deserialization of the bean Customer. Your programs knows how to do it in XML, with JAXB as Daniel is writing, but most likely doesn't know how to do it in JSON.

    Here you have an example with Resteasy/Jackson http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/

    The same with Jersey: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

    0 讨论(0)
  • 2020-11-27 05:45

    just change the content-type to application/json when you use JSON with POST/PUT, etc...

    0 讨论(0)
  • 2020-11-27 05:50

    I had the same problem:

    curl -v -H "Content-Type: application/json" -X PUT -d '{"name":"json","surname":"gson","married":true,"age":32,"salary":123,"hasCar":true,"childs":["serkan","volkan","aybars"]}' XXXXXX/ponyo/UserModel/json

    * About to connect() to localhost port 8081 (#0)
    *   Trying ::1...
    * Connection refused
    *   Trying 127.0.0.1...
    * connected
    * Connected to localhost (127.0.0.1) port 8081 (#0)
    > PUT /ponyo/UserModel/json HTTP/1.1
    > User-Agent: curl/7.28.1
    > Host: localhost:8081
    > Accept: */*
    > Content-Type: application/json
    > Content-Length: 121
    > 
    * upload completely sent off: 121 out of 121 bytes
    < HTTP/1.1 415 Unsupported Media Type
    < Content-Type: text/html; charset=iso-8859-1
    < Date: Wed, 09 Apr 2014 13:55:43 GMT
    < Content-Length: 0
    < 
    * Connection #0 to host localhost left intact
    * Closing connection #0
    

    I resolved it by adding the dependency to pom.xml as follows. Please try it.

        <dependency>
            <groupId>com.owlike</groupId>
            <artifactId>genson</artifactId>
            <version>0.98</version>
        </dependency>
    
    0 讨论(0)
  • 2020-11-27 05:58

    I encountered the same issue in postman, i was selecting Accept in header section and providing the value as "application/json". So i unchecked and selected Content-Type and provided the value as "application/json". That worked!

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