REST Webservice returning 415 - Unsupported Media Type

104,269

Solution 1

You need to send the request-header Content-Type: application/json. Seems like REST-Client does not add this header automatically for you.

Solution 2

As others have pointed out, you are missing the correct header. Add Content-Type: application/json to the "Headers": enter image description here

Solution 3

I had the same 415 http error time ago. I simply forgot the default no-parameters constructor in my DTO classes. Adding that constructor, in a similar way as for JPA entities, solved my issue and de-serialization JSON->Object works now.

I'm not sure this is your case, looking at your code, but it could be useful to other guys falling here looking at the 415+JSON issue. Regards

Solution 4

Add

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

in REST Client header section

Solution 5

Ok so there are error codes that commonly appear during content negotiation 1) 406 - Not acceptable 2) 415 - Unsupported Media Type

406 is when the server doesn't accept the content type that is being sent under the ACCEPT header of the request.

415 is the client is sending a content-type in the request header and so server straightforwardly rejects saying unsupported media type

to overcome 406 - we must include the appropriate dependent jars say a client wants an XML input to be understood by the server, then the server needs to have XML related dependencies.

to overcome 415 - understand the media types that are supported by the server and pass the correct media type in the content-type header of the request

Share:
104,269

Related videos on Youtube

Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I've created a REST webservice using jax-rs and jersey that is supposed to consume JSON on a POST request. My web service class looks like this:

    @Path("/webhookservice")
    public class Webhook {
    
        @POST
        @Consumes(MediaType.APPLICATION_JSON)
        public Response readData (Song song) {
    
            // Prints out the song info
            System.out.println("SONG INFO \n=======================");
            System.out.println("songname: " + song.getSongname());
            System.out.println("artist: " + song.getArtist());
    
            // Repsonse with a HTTP 200 OK
            Response response = Response.status(200).build();
            return response;
    
        }
    
    }
    

    My Song class:

    public class Song {
    
        private String songname;
        private String artist;
    
        public String getSongname () { return this.songname; }
        public String getArtist () { return this.artist; }
    
        public void setSongname (String songname) { this.songname = songname; }
        public void setArtist (String artist) { this.artist = artist; }
    
    }
    

    My web.xml (if needed)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
    
        <servlet>
            <servlet-name>SnapScan-Webhook</servlet-name>
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>com.sun.jersey.config.property.packages</param-name>
                <param-value>za.co.lancet.service</param-value>
            </init-param>
            <init-param>
                <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
                <param-value>true</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>SnapScan-Webhook</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    I'm using RESTClient a little, well, rest client... Here's a screenshot of what I'm sending:

    enter image description here

    When I send that off, I get the 415 Unsupported Media Type error. Anybody have an idea why?

    • lefloh
      lefloh almost 10 years
      Is the client sending the header Content-Type: application/json?
    • Admin
      Admin almost 10 years
      You know what you might be right. Let me see
    • Admin
      Admin almost 10 years
      @lefloh - You are the winner! You were right, I didn't even know you need to set that in these little client apps. You can post as an answer and I'll accept. Thanks!
  • Admin
    Admin almost 10 years
    As far as I know this is optional but not required. I'll give it a try nontheless
  • Sarvesh
    Sarvesh almost 8 years
    How to add content type programmatically in android
  • Michał Piątkowski
    Michał Piątkowski about 6 years
    Your answer saved me a lot of debugging :D
  • Ibo
    Ibo about 6 years
    Theasker had some code, could you please answer him using his code and show him where to fix it?