title: Spring @MVC and the @RequestBody annotation with x-www-form-urlencoded data?
body: <p>I am trying to figure out why I can't receive a request from a jQuery.ajax call when then Spring <code>@Controller</code> handler method includes a <code>@RequestBody</code> annotation. Consider the following:</p>
  
  <p><strong>HTML/JavaScript</strong>:</p>
  
  <pre><code>&lt;form id="foo" action="/baz"&gt;
    &lt;input name="bar"&gt;
  &lt;/form&gt;
  
  &lt;script&gt;
    $(function() {
      var $fooForm = $('#foo');
  
      $fooForm.on('submit', function(evt) {
        evt.preventDefault();
  
        $.ajax({
          url: $fooForm.action,
          data: $fooForm.serialize(),
          dataType: 'json',
          type: 'POST',
          success: function(data) { console.log(data); }
        });
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Java</strong>:</p>
  
  <pre><code>@RequestMapping(
    value = "/baz",
    method = RequestMethod.POST,
    consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
    produces = MediatType.APPLICATION_JSON_VALUE
  )
  public @ResponseBody SearchResults[] jqueryPostHandler(
    @RequestBody FormDataObject formData)
  {
    return this.searchService.find(formData);
  }
  </code></pre>
  
  <p>The above will fail with the <code>@RequestBody</code> annotation present and return a 415 error (no exception will be generated). But if the <code>@RequestBody</code> annotation is removed (i.e. the parameter signature is just <code>FormDataObject formData</code>) then the method will be called and JSON will be returned to the JavaScript.</p>
  
  <p>Why is this the case? A <code>POST</code> request includes the data in the body of the request. Shouldn't the annotation process such a request?</p>
  
  <p>I realize that I could change the content type sent by the JavaScript to <code>application/json</code> and the <code>consumes</code> property to <code>MediaType.APPLICATION_JSON_VALUE</code> to make the annotation work correctly. But why doesn't it work with a normal form request?</p>
  
  <p><strong>Note</strong>: I am using Spring 3.1.4.</p>