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.
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.
As the Spring Docs of @RequestBody says, the request body would be converted to your object by HttpMessageConverter
.
There are 4 default HttpMessageConverters:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With