title: 415 Unsupported MediaType for POST request in spring application
body: <p>I have a very simple <code>Spring</code> Application (NOT spring boot). I have implemented a GET and POST controller methods. the <code>GET</code> method works fine. But the <code>POST</code> is throwing <code>415 Unsupported MediaType</code>. Steps to reproduce are available below</p>
  
  <p><code>ServiceController. java</code></p>
  
  <pre><code>package com.example.myApp.controller;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseBody;
  
  
      @Controller
      @RequestMapping("/service/example")
      public class ServiceController {
  
          @RequestMapping(value="sample", method = RequestMethod.GET)
          @ResponseBody
      public String getResp() {
          return "DONE";
      }
  
      @RequestMapping(value="sample2", method = RequestMethod.POST, consumes = "application/json")
      @ResponseBody
      public String getResponse2(@RequestBody Person person) {
          return "id is " + person.getId();
      }
  
  }
  
  class Person {
  
      private int id;
      private String name;
  
      public Person(){
  
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p><code>AppConfig.java</code></p>
  
  <pre><code>package com.example.myApp.app.config;
  
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  @Configuration
  @EnableWebMvc
  @ComponentScan("com.example.myApp")
  public class AppConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/test/**").addResourceLocations("/test/").setCachePeriod(0);
          registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0);
          registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(0);
          registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(0);
      }
  }
  </code></pre>
  
  <p><code>AppInitializer.java</code></p>
  
  <pre><code>package com.example.myApp.app.config;
  
  import org.springframework.web.WebApplicationInitializer;
  import org.springframework.web.context.ContextLoaderListener;
  import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
  import org.springframework.web.servlet.DispatcherServlet;
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRegistration;
  
  public class AppInitializer implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
  
  
          // Create the 'root' Spring application context
          AnnotationConfigWebApplicationContext rootContext =
                  new AnnotationConfigWebApplicationContext();
  
          rootContext.register(AppConfig.class);
          servletContext.addListener(new ContextLoaderListener(rootContext));
  
          // Register and map the dispatcher servlet
          ServletRegistration.Dynamic dispatcher =
                  servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
  
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
  
      }
  
  
  }
  </code></pre>
  
  <p>The code is available here: </p>
  
  <pre><code>git clone https://bitbucket.org/SpringDevSeattle/springrestcontroller.git
  ./gradlew clean build tomatrunwar
  </code></pre>
  
  <p>This spins up embedded tomcat. </p>
  
  <p>Now you can curl the following</p>
  
  <pre><code>curl -X GET -H "Content-Type: application/json" "http://localhost:8095/myApp/service/example/sample"
  </code></pre>
  
  <p>works fine</p>
  
  <p>But </p>
  
  <pre><code>curl -X POST -H "Content-Type: application/json" '{
      "id":1,
      "name":"sai"
  }' "http://localhost:8095/myApp/service/example/sample2"
  </code></pre>
  
  <p>Throws 415 unsupported MediaType </p>
  
  <pre><code>&lt;body&gt;
          &lt;h1&gt;HTTP Status 415 - &lt;/h1&gt;
          &lt;HR size="1" noshade="noshade"&gt;
          &lt;p&gt;
              &lt;b&gt;type&lt;/b&gt; Status report
          &lt;/p&gt;
          &lt;p&gt;
              &lt;b&gt;message&lt;/b&gt;
              &lt;u&gt;&lt;/u&gt;
          &lt;/p&gt;
          &lt;p&gt;
              &lt;b&gt;description&lt;/b&gt;
              &lt;u&gt;The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.&lt;/u&gt;
          &lt;/p&gt;
          &lt;HR size="1" noshade="noshade"&gt;
          &lt;h3&gt;Apache Tomcat/7.0.54&lt;/h3&gt;
      &lt;/body&gt;
  </code></pre>