1 @RequestBody 处理类型
在项目中经常看到controller 中有 @RequestBody 字样,他到底有什么作用?
一般使用表单提交数据时不需要使用@RequestBody 即可自动封装数据到对应的 Bean 中。@RequestBody 用来处理Content-Type: application/json, application/xml等
它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。
说明:使用 @RequestBody 解析数据需要添加 jackson 或 fastjson 依赖包。
maven 引入 fastjson 包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency>
2 对象 和 json 相互转换
在项目中经常会遇到对象和 json 之间相互转换,公共类 和 json 对象转换, 静态内部类 和 json 对象转换
2.1 没有内部类时 Student 类
@Data public class Student { private String id; private String name; private