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

前端 未结 13 1774
渐次进展
渐次进展 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:31

    I had the same issue, as it was giving error-415-unsupported-media-type while hitting post call using json while i already had the

    @Consumes(MediaType.APPLICATION_JSON) 
    

    and request headers as in my restful web service using Jersey 2.0+

    Accept:application/json
    Content-Type:application/json
    

    I resolved this issue by adding following dependencies to my project

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

    this will add following jars to your library :

    1. jersey-media-json-jackson-2.25.jar
    2. jersey-entity-filtering-2.25.jar
    3. jackson-module-jaxb-annotations-2.8.4.jar
    4. jackson-jaxrs-json-provider-2.8.4.jar
    5. jackson-jaxrs-base-2.8.4.jar

    I hope it will works for others too as it did for me.

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

    If none of the solution worked and you are working with JAXB, then you need to annotate your class with @XmlRootElement to avoid 415 unsupported media type

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

    Just in case this is helpful to others, here's my anecdote:

    I found this thread as a result of a problem I encountered while I was using Postman to send test data to my RESTEasy server, where- after a significant code change- I was getting nothing but 415 Unsupported Media Type errors.

    Long story short, I tore everything out, eventually I tried to run the trivial file upload example I knew worked; it didn't. That's when I realized that the problem was with my Postman request. I normally don't send any special headers, but in a previous test I had added a "Content-Type": "application/json" header. OF COURSE, I was trying to upload "multipart/form-data." Removing it solved my issue.

    Moral: Check your headers before you blow up your world. ;)

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

    I get the same issue. In fact, the request didn't reach the jersey annoted method. I solved the problem with add the annotation: @Consumes(MediaType.APPLICATION_FORM_URLENCODED) The annotation @Consumes("/") don't work!

        @Path("/"+PropertiesHabilitation.KEY_EstNouvelleGH)
        @POST
        //@Consumes("*/*")
        @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
        public void  get__EstNouvelleGH( @Context HttpServletResponse response) {
            ...
        }
    
    0 讨论(0)
  • 2020-11-27 05:35

    Don't return Strings in your methods but Customer objects it self and let JAXB take care of the de/serialization.

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

    In case you are trying to use ARC app from google and post a XML and you are getting this error, then try changing the body content type to application/xml. Example here

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