Http Post with request content type form not working in Spring MVC 3 -
code snippet:
@requestmapping(method = requestmethod.post)//, headers = "content-type=application/x-www-form-urlencoded") public modelandview create(@requestbody useraccountbean account) { try{ accounts.put(account.assignid(), account); }catch(runtimeexception ex) { return new modelandview("account/registererror"); } return new modelandview("account/userverification"); }
after receiving request, got http status code 415: server refused request because request entity in format not supported requested resource requested method ().
if change code this:
code snippet:
@requestmapping(method = requestmethod.post,headers = "content-type=application/x-www-form-urlencoded") public modelandview create(@requestbody useraccountbean account) { try{ accounts.put(account.assignid(), account); }catch(runtimeexception ex) { return new modelandview("account/registererror"); } return new modelandview("account/userverification"); }
i 405 method not allowed. funny thing in allow header of response, lists , post allowed methods.
i have class josn mapping:
@component public class jacksonconversionserviceconfigurer implements beanpostprocessor {
private final conversionservice conversionservice; @autowired public jacksonconversionserviceconfigurer(conversionservice conversionservice) { this.conversionservice = conversionservice; } public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { return bean; } public object postprocessafterinitialization(object bean, string beanname) throws beansexception { if (bean instanceof annotationmethodhandleradapter) { annotationmethodhandleradapter adapter = (annotationmethodhandleradapter) bean; httpmessageconverter<?>[] converters = adapter.getmessageconverters(); (httpmessageconverter<?> converter : converters) { if (converter instanceof mappingjacksonhttpmessageconverter) { mappingjacksonhttpmessageconverter jsonconverter = (mappingjacksonhttpmessageconverter) converter; jsonconverter.setobjectmapper(new conversionserviceawareobjectmapper(this.conversionservice)); } } } return bean; }
}
copied spring examples. works great json content-type.
a more general question how make spring mvc request handlers work different request content-types. advice appreciated.
unfortunately formhttpmessageconverter
(which used @requestbody
-annotated parameters when content type application/x-www-form-urlencoded
) cannot bind target classes (as @modelattribute
can).
therefore need @modelattribute
instead of @requestbody
. if don't need pass different content types method can replace annotation:
@requestmapping(method = requestmethod.post) public modelandview create(@modelattribute useraccountbean account) { ... }
otherwise guess can create separate method form processing form data appropriate headers
attribute:
@requestmapping(method = requestmethod.post, headers = "content-type=application/x-www-form-urlencoded") public modelandview createfromform(@modelattribute useraccountbean account) { ... }
edit: possible option implement own httpmessageconverter
combining formhttpmessageconverter
(to convert input message map of parameters) , webdatabinder
(to convert map of parameters target object).
Comments
Post a Comment