Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @MVC and the @RequestBody annotation with x-www-form-urlencoded data?

I am trying to figure out why I can't receive a request from a jQuery.ajax call when then Spring @Controller handler method includes a @RequestBody annotation. Consider the following:

HTML/JavaScript:

<form id="foo" action="/baz">
  <input name="bar">
</form>

<script>
  $(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); }
      });
    });
  });
</script>

Java:

@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);
}

The above will fail with the @RequestBody annotation present and return a 415 error (no exception will be generated). But if the @RequestBody annotation is removed (i.e. the parameter signature is just FormDataObject formData) then the method will be called and JSON will be returned to the JavaScript.

Why is this the case? A POST request includes the data in the body of the request. Shouldn't the annotation process such a request?

I realize that I could change the content type sent by the JavaScript to application/json and the consumes property to MediaType.APPLICATION_JSON_VALUE to make the annotation work correctly. But why doesn't it work with a normal form request?

Note: I am using Spring 3.1.4.

like image 799
James Sumners Avatar asked May 08 '13 19:05

James Sumners


2 Answers

Have you tried turning up logging on 'org.springframework.web' to find out the reason for the returned status code? There should be an exception raised and logged before it's translated to a 415.

Also if sending form data, why not just leave out the @RequestBody. You'll then be use data binding (i.e. @ModelAttribute) that will apply Servlet request parameters to object fields. This is preferable to using the FormHttpMessageConverter.

like image 80
Rossen Stoyanchev Avatar answered Sep 17 '22 17:09

Rossen Stoyanchev


As the Spring Docs of @RequestBody says, the request body would be converted to your object by HttpMessageConverter.

There are 4 default HttpMessageConverters:

  • ByteArrayHttpMessageConverter: converts byte arrays.
  • StringHttpMessageConverter: converts strings.
  • FormHttpMessageConverter: converts form data to/from a MultiValueMap.
  • SourceHttpMessageConverter: converts to/from a javax.xml.transform.Source.

To convert the url encoded form, which is ajax.serialize() create, it's the job of FormHttpMessageConverter. Since you have a HttpMediaTypeNotSupportedException exception, I guess that you didn't configure FormHttpMessageConverter. Check your spring configuration file, here is an example :

< bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
   < property name= "messageConverters" >
         < list>
             < ref bean= "mappingJacksonHttpMessageConverter" />
             < ref bean= "stringHttpMessageConverter" /> 
             <!-- Do you have this converter ? -->
             < ref bean= "formHttpMessageConverter" />
         </ list>
    </ property>
</ bean>
like image 40
Qianyue Avatar answered Sep 20 '22 17:09

Qianyue