jquery - Spring @MVC and the @RequestBody annotation with x-www-form-urlencoded data? -
i trying figure out why can't receive request jquery.ajax call when spring @controller
handler method includes @requestbody
annotation. consider 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 fail @requestbody
annotation present , return 415 error (no exception generated). if @requestbody
annotation removed (i.e. parameter signature formdataobject formdata
) method called , json returned javascript.
why case? post
request includes data in body of request. shouldn't annotation process such request?
i realize change content type sent javascript application/json
, consumes
property mediatype.application_json_value
make annotation work correctly. why doesn't work normal form request?
note: using spring 3.1.4.
have tried turning logging on 'org.springframework.web' find out reason returned status code? there should exception raised , logged before it's translated 415.
also if sending form data, why not leave out @requestbody. you'll use data binding (i.e. @modelattribute) apply servlet request parameters object fields. preferable using formhttpmessageconverter.
Comments
Post a Comment