title: Spring MVC @RequestBody not working with jquery ajax?
body: <p>Here is my ajax request  </p>
  
  <pre><code>var dataModel = {name1:"value1", name2:"value2"};
  
  $.ajax({
                url: "/testURL",
                type: "POST",
                async: false,
                contentType: "application/json",
                data: dataModel,
                success: function(response) {
  
                }
  
      })
  </code></pre>
  
  <p>Here is my relevant snippet from spring xml </p>
  
  <pre><code>&lt;annotation-driven&gt;
          &lt;!-- Message converters are added to use custom object mapper in  MappingJackson2HttpMessageConverter.
              StringHttpMessageConverter is required to avoid MappingJackson2HttpMessageConverter from converting a string into json.
          --&gt;
          &lt;message-converters&gt;
              &lt;beans:bean
                  class="org.springframework.http.converter.StringHttpMessageConverter"&gt;
              &lt;/beans:bean&gt;
              &lt;beans:bean
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
                  &lt;beans:property name="objectMapper" ref="jacksonObjectMapper"/&gt;
              &lt;/beans:bean&gt;
          &lt;/message-converters&gt;
      &lt;/annotation-driven&gt;
  </code></pre>
  
  <p>Here is my controller mapping</p>
  
  <pre><code>    @RequestMapping(value = "/testURL", method = { RequestMethod.POST })
      public String add(HttpServletRequest request, @RequestBody CustomObject customObject) throws Exception {}
  </code></pre>
  
  <p>But My request does not even reach to controller. As soon as I remove <code>@RequestBody CustomObject customObject</code> it works. But I want to map the json request to 
      CustomObject with <code>@RequestBody</code> which is not happening . <strong>Not sure what i am missing here ?</strong></p>
  
  <p>In fact when I inspect <code>request.getParameterMap()</code> it displays empty but as soon as I remove <code>contentType: "application/json"</code> I see parameter map gets populated but still
      then get below error</p>
  
  <pre><code>`The server refused this request because the request entity is in a format not supported by the requested resource for the requested method`
  </code></pre>
  
  <p>Here is my  CustomObject definition</p>
  
  <pre><code>public class CustomObject implements Serializable {
  
  private static final long serialVersionUID = 1L;
  private String name1;
  private String name2;
  
  //getters and setters
  }
  </code></pre>
  
  <p>Already gone through <a href="https://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together/5908632#5908632">JQuery, Spring MVC @RequestBody and JSON - making it work together</a> but did not help</p>