415 Unsupported Media Type

415 Unsupported Media Type

415 Unsupported Media Type

You see this error when working with AngualarJS and Spring MVC web application while posting data using AngularJS http service.

Note: If you came from JQuery background and thinking about swapping jQuery.post() with AngularJS $http.post() will not work because both serialize and transmit data in different format:

  • JQuery data serialization : headers: {‘Content-Type’: ‘application/x-www-form-urlencoded’}
  • AngularJS data serialization : headers: {‘Content-Type’: ‘application/json’}

Possible reasons: 

  • You have not included Jackson libraries in your classpath
  • When you send request from AngularJS to Spring MVC controller JSON converts to Java Object using message converter. Check your spring XML configuration if you have added below tag or not
<mvc:annotation-driven/>
  • Probably JSON data not correctly created. Please check your JSON object before sending request to the controller. You could use JSONLint to validate your JSON data format.
  • Check JSON object property name is correctly matching with your Spring MVC controller domain object
  • Finally check your request mapping in controller it should be as below if you have example domain name person:
@RequestMapping(value = "/PostService", method = RequestMethod.POST)
	public @ResponseBody String PostService(@RequestBody Person person) {}
4 thoughts on “415 Unsupported Media Type”
  1. I was using Angular with Spring and getting same exception.
    After putting Jackson lib, exception is resolved

  2. use this Filter
    @Priority(Priorities.HEADER_DECORATOR)
    @Provider
    public class HeadersReponseFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest creq, ContainerResponse response) {
    response.getHttpHeaders().putSingle(“Access-Control-Allow-Origin”, “*”);
    response.getHttpHeaders().putSingle(“Access-Control-Allow-Headers”,
    “Access-Control-Allow-Origin,crossDomain,origin, content-type, accept,dataType, authorization”);
    response.getHttpHeaders().putSingle(“Access-Control-Max-Age”, “2000”);
    response.getHttpHeaders().putSingle(“crossDomain”, true);
    response.getHttpHeaders().putSingle(“Access-Control-Allow-Credentials”, true);
    response.getHttpHeaders().putSingle(“Access-Control-Allow-Methods”, “GET, POST, PUT, DELETE, OPTIONS, HEAD”);
    return response;
    }
    }

    put in the web.xml

    com.sun.jersey.spi.container.ContainerResponseFilters
    YourPackage.HeadersReponseFilter

Leave a Reply

Your email address will not be published. Required fields are marked *