使用目的:
常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是application/xml等。一般情况下来说常用其来处理application/json类型。
简单例子:
@RestController
@RequestMapping("/sys")
public class BaseController {
@PostMapping("/placement")
public String testPlace(@RequestBody String name) {//这里是可以用对象去接收处理的
//Placement placement = new Placement();
//placement.setId(Long.valueOf(id));
//placement.setName(name);
return JSON.toJSONString(name);
}
前端请求体 (idea的写法)
POST localhost:8080/pathfinder/sys/placement
Content-Type: application/json
{
id:'1223'
name:'duand'
}
使用注意点:
一般不支持多个@requestBody的使用!!!
注意两个地方:1)请求头中的content-type; 2) 请求方式。
a. content-type : multipart/form-data
这种格式使用@RequestBody处理不了。
b. content-type : application/x-www-form-urlencoded
form表单形式提交的数据格式,可以使用@RequestBody,也可以使用其他注解例如@RequestParam, @ModelAttribute进行接收。
c. content-type : 其他数据格式
建议使用@RequestBody进行接收。
原理:
一句话总结:
由HttpMessageConverter(多个实现类)转换http信息成对应的java对象供springMVC使用
参考:
https://blog.csdn.net/qq_34500957/article/details/80523200
https://www.cnblogs.com/linjiaxin/p/9297833.html
https://blog.csdn.net/danielzhou888/article/details/72861097