title: 400 Bad Request on ajax post with @RequestBody using Jackson
body: <p>I'm getting 400 Bad Request on Ajax request when trying to map my JSON to a Java object in spring MVC controller. I have checked most of the relevant questions in the topic but still couldn't make it work</p>
  
  <p>Ajax call and JSON:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "vocabulary/createVocabulary",
      contentType : 'application/json; charset=utf-8',
      data: {"vocabularyName" : "a",
      "vocabularyDescription" : "b"},
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>@Service
  @Controller
  @RequestMapping(path="vocabulary")
  public class VocabularyController {
  
  @RequestMapping (path = "createVocabulary", method = RequestMethod.POST)
  @ResponseBody String createVocabulary(@RequestBody VocabularyDTO vocabularyDTO){
      return "Success";
    }
  }
  </code></pre>
  
  <p>My Java object:</p>
  
  <pre><code>public class VocabularyDTO {
  
  private String vocabularyName;
  private String vocabularyDescription;
  
  public VocabularyDTO(){};
  
  public String getVocabularyName() {
      return vocabularyName;
  }
  
  public void setVocabularyName(String vocabularyName) {
      this.vocabularyName = vocabularyName;
  }
  
  public String getVocabularyDescription() {
      return vocabularyDescription;
  }
  
  public void setVocabularyDescription(String vocabularyDescription) {
      this.vocabularyDescription = vocabularyDescription;
  }
  }
  </code></pre>
  
  <p>I use Spring 4.2.5 and Jackson:</p>
  
  <pre><code>...
  
  def springVersion = "4.2.5.RELEASE"
  
  repositories {
  mavenCentral()
  jcenter()
  }
  
  dependencies {
  
  compile group: "org.springframework", name: "spring-core", version: "$springVersion"
  compile group: "org.springframework", name: "spring-beans", version: "$springVersion"
  compile group: "org.springframework", name: "spring-context", version: "$springVersion"
  compile group: "org.springframework", name: "spring-aop", version: "$springVersion"
  compile group: "org.springframework", name: "spring-web", version: "$springVersion"
  compile group: "org.springframework", name: "spring-webmvc", version: "$springVersion"
  compile group: "org.springframework.data", name: "spring-data-jpa", version: "1.9.4.RELEASE"
  
  compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.6.5"
  compile group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.6.5"
  compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.6.5"
  
  ...
  }
  </code></pre>
  
  <p>The error I'm getting:</p>
  
  <blockquote>
    <p>HTTP ERROR 400
    Problem accessing /ux/vocabulary/createVocabulary. Reason:
    BAD_REQUEST</p>
  </blockquote>
  
  <p>Additionally, if I remove the '@RequestBody'annotation from my controller I get the following server response:</p>
  
  <blockquote>
    <p>Failed to instantiate [com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO.()</p>
  </blockquote>
  
  <p>Does anyone have any idea what could be wrong? Thanks!</p>
  
  <p>UPDATE:
  The option without '@RequestBody' annotation is actually working now (it was a silly mistake of mine why it didn't before) - i.e. not throwing error, however without the values being passed to the object.</p>
  
  <p>I was hoping it would fix the annotation as well but I still get the 400 error that way.</p>