在SpringMvc中参数会根据url参数自动注入到action方法内。
此时如果参数需要接收复杂对象,则必需在参数声明前添加@RequestBody,如下:
@RequestMapping("test")
public Model test(@RequestBody Client client, Model model){
return model;
}
在org.springframework.web.bind.annotation.support.HandlerMethodInvoker中的方法resolveHandlerArguments作用就是解析给test方法中适配参数。
用resolveRequestBody方法填充来填充带@RequestBody的参数
resolveRequestBody的功能是用messageConverters对请求体进行read操作。
ajax提交如下:
$.ajax({
url: '/test',
contentType: 'application/json',
data: JSON.stringify(client),
dataType: 'json',
success: function(data) {
console.log(data)
},
type: "POST"
});
后台messageConverter采用
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />