作用:
- 注解用于将Controller的方法参数,根据HTTP Request Header的
content-Type
的内容,通过适当的HttpMessageConverter转换为JAVA类
springmvc controller例子:
@PostMapping("/changeMerchant")
public ApiResult changeMerchant(@RequestBody @Valid MerchantAddParam param, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return ApiResult.paramsError(bindingResult.getFieldError().getField());
}
ApiResult apiResult = merchantService.addOrUpdateMerchant(param);
return apiResult;
}
页面js请求例子:
$.ajax({
url: "changeMerchant",
data: '{"name":"小红","age":12}', //要用双引号!!
contentType: "application/json;charset=utf-8", // 因为上面是JSON数据
type: "POST",
headers: {
Accept: "application/json",
},
success: function(data, textStatus){
console.log(data);
alert(data);
}
});
POST
数据是JSON格式, 而不是普通的键值对形式.
如果是form表单 键值对格式请求 会报错:
Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported