Jersey 415 Unsupported Media Type

415 unsupported media type web api
415 (unsupported media type) angular 8
415 unsupported media type json
415 unsupported media type retrofit android
curl unsupported media type
unsupported media type: application/json in request
how to fix unsupported media type
unsupported media type postman java

I have been trying since hours to correct http error 415 Unsupported Media Type but it is still showing media unsupported page. I am adding headers application/json in Postman.

Here is my Java Code

package lostLove;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;  
import javax.ws.rs.POST;
import javax.ws.rs.Path;  
import javax.ws.rs.PathParam;  
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; 

import org.json.JSONObject;


@Path("/Story") 
public class Story {

      @POST
      @Consumes({"application/json"})
      @Produces(MediaType.APPLICATION_JSON)
    //  @Consumes(MediaType.APPLICATION_JSON)
    //  @Path("/Story") 
      public JSONObject sayJsonTextHello(JSONObject inputJsonObj) throws Exception {

        String input = (String) inputJsonObj.get("input");
        String output = "The input you sent is :" + input;
        JSONObject outputJsonObj = new JSONObject();
        outputJsonObj.put("output", output);

        return outputJsonObj;
      }

      @GET  
      @Produces(MediaType.TEXT_PLAIN)  

      public String sayPlainTextHello() {  
        return "hello";
      }

}

here is my web.xml file

<?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">
  <display-name>LostLove</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 <servlet>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>  
    <init-param>  
        <param-name>jersey.config.server.provider.packages</param-name>  
        <param-value>lostLove</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>Jersey REST Service</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
  </servlet-mapping>
</web-app>

REST API: "Error 415 Unsupported Media Type", REST API: "Error 415 Unsupported Media Type" 14:19:37,263 ERROR [​btpool0-2 ] com.atlassian.plugins.rest.common.error.jersey. You have the jersey-media-json-jackson on the classpath, but you will still need to register the provider. That's why the Unsupported Media Type. If Jersey can't find a provider to handle the conversion, this is the the status you will get. If you are using web.xml, you can use

Its because of following issue:

JAX-RS does not support default Jackson mapping conversion. So if you have the ajax request as below(Post):

 jQuery.ajax({
           url: "http://localhost:8081/EmailAutomated/rest/service/save",
            type: "POST",
            dataType: "JSON",
            contentType: "application/JSON",
            data: JSON.stringify(data),
            cache: false,
            context: this,
            success: function(resp){  
                 // we have the response  
                 alert("Server said123:\n '" + resp.name + "'");  
               },  
               error: function(e){  
                 alert('Error121212: ' + e);  
               }  
        });

and in JAX-RS controller side you need to do like below:

@Path("/save")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String saveDetailsUser(String userStr) {

    Gson gson = new Gson();
    UserDetailDTO userDetailDTO = gson.fromJson(userStr, UserDetailDTO.class);

    String vemail = userDetailDTO.getEMAIL();

    return "userDetailDTO";
}

Here please make sure on parameter. service is accepting json as String not the POJO.

Surely It will work. Thanks!

415: Unsupported media type (while consuming JSON input to JAX , 415: Unsupported media type (while consuming JSON input to JAX-RS service) 'org.glassfish.jersey.media', name: 'jersey-media-json-jackson', version: '2.9'. jersey-json provides a set of MessageBodyReader<T> and MessageBodyWriter<T>. You may add @XmlRootElement to your NTReport or choose any other option available for jersey 1.x since neo4j community server uses Jersey 1.9 at the moment

I have seen the same problem when using Jersey with HTTP/2, if the client send HTTP/1.1 request,e.g. using Jersey client, then it works fine.

If I switch to Jetty HTTP2 Client to send the same content, I get 415.

The temp solution I use is the alternative described by Paul Samsotha, i.e. "accept a String and return a String", then manually deserialize the String to POJO.

415 Unsupported Media Type - HTTP, The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload  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

HTTP 415 Unsupported Media Type when requesting restful api's , But I encountered HTTP status 415 Unsupported Media Type and I ServerRuntime$1.run(ServerRuntime.java:269)","org.glassfish.jersey. # A 415 Unsupported Media Type error occurs when the origin server refuses a particular request since the resource is in a format that is not supported by the server for the HTTP method used. This unsupported format type issue can be caused by what is defined in the resource's Content-Type or Content-Encoding headers.

Using @Consumes and @Produces to Customize Requests and , If @Consumes is applied at the method level, it overrides any @Consumes annotations applied at the class level. If a resource is unable to consume the MIME type of a client request, the Jersey runtime sends back an HTTP “415 Unsupported Media Type” error. 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 works fine for me, also the POST (when it’s a application/xml) but when I try application/json it doesn’t not reach the WS at all, the server rejects it.

Enable JSON conversion in MOXy to avoid 415 Unsupported Media , Enable JSON conversion in MOXy to avoid 415 Unsupported Media Type. If you follow the “Getting Started” tutorial for Jersey (an API for writing  <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.18</version> </dependency> No warnings/errors at compilation but 415 Unsuported Media Type at runtime trying to consum JSON on Jersey 2.18 running on AppEngine Hope it helps

Comments
  • This solved my problem but now I am getting some other error =>unrecognized field (class org.json.jsonobject) not marked as ignorable. I searched on net and added JsonIgnore to method and @JsonIgnoreProperties(ignoreUnknown = true) to story class but I am still getting the same error. Thanks for the help.
  • I did not say to use JSONObject with Jackson. If you are using Jackson, use simple POJOS like my Model class. The internals of JSONObject are not in JavaBean style, which is required
  • See the Jackson Tutorial for en example of how your POJOs should look.
  • "JAX-RS does not support default Jackson mapping conversion." Can you please clarify it?
  • It might be the problem of old versions of JAX-RS. I am using jersey 1.8.3 and specification says it does not convert json(coming from Ajax post) to POJO automatically.
  • That's not accurate: In Jersey 1.x you can enable the JSONConfiguration.FEATURE_POJO_MAPPING and Jackson will be used to convert JSON to Java Objects and back.
  • Thanks for the information. But If we don't enable then Its does not make any conversion. So I answered here because might be someone else will face similar problem. Thanks Cassio!
  • "I answered here because might be someone else will face similar problem." The OP is using Jersey 2.x instead of Jersey 1.x. See org.glassfish.jersey in the web.xml.