title: 400 Bad Request - while POSTing JSON data to RESTful controller implemented with Spring MVC
body: <p>I am trying to POST some JSON data to RESTful spring controller. But getting "400 Bad Request" as response status.</p>
  
  <p>Giving below code from the key configuration files which I am using for this:</p>
  
  <p><strong>pom.xml dependencies:</strong></p>
  
  <pre><code>&lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;4.0.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;4.0.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;4.0.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
              &lt;version&gt;2.3.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;2.3.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.8.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
              &lt;version&gt;3.1.2.RELEASE&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>
  
  <p><strong>dispatcher-servlet.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.lokur.controllers" /&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
          &lt;property name="mediaTypes"&gt;
              &lt;map&gt;
                  &lt;entry key="json" value="application/json" /&gt;
                  &lt;entry key="xml" value="text/xml" /&gt;
              &lt;/map&gt;
          &lt;/property&gt;
          &lt;property name="defaultContentType" value="text/html" /&gt;
          &lt;property name="defaultViews"&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Controller class:</strong></p>
  
  <pre><code>package com.lokur.controllers;
  
  import com.lokur.dto.Employee;
  import org.springframework.http.HttpStatus;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.*;
  
  import javax.servlet.http.HttpServletResponse;
  
  @Controller
  @RequestMapping("/employees")
  public class EmployeeController {
  
      @RequestMapping(method = RequestMethod.POST)
      @ResponseStatus(HttpStatus.CREATED)
      public void saveEmployee(@RequestBody Employee employee, HttpServletResponse response) {
  
          System.out.println("Saving employee...");
          System.out.println("Emp ID = " + employee.getId());
          System.out.println("Emp Name = " + employee.getName());
  
          // Set dummy "location" for this newly created Employee resource in response header
          response.setHeader("Location", "/employees/"+employee.getId());
      }
  
  }
  </code></pre>
  
  <p><strong>Request details:</strong></p>
  
  <p><em>URL:</em> http: //localhost:8081/RestfulWSPOC/employees</p>
  
  <p><em>Method:</em> POST</p>
  
  <p><em>Headers:</em> Accept: application/json</p>
  
  <p><em>Payload:</em></p>
  
  <pre><code>{
      id: 300999
  
      name: "Akshay Lokur"
  
      designation: "Associate"
  
      department: "Tech"
  
      salary: 500000
  }
  </code></pre>
  
  <p>Thanks.</p>