github地址: https://github.com/liufeiSAP/JavaWebStudy
实体类:
package com.study.demo.domain; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; public class Student { @JsonProperty(value="anothername") private String name; private int age; private List<Course> courses; public String getName() { return name; } public int getAge() { return age; } public List<Course> getCourses() { return courses; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setCourses(List<Course> courses) { this.courses = courses; } }
Controller:
第一种方法: 使用@RequestBody(推荐),springboot集成了jackson,可以自动把json转成对象;
(注意:json的key的名字要和实体属性的名字一样(如果不一样要加上@JsonProperty 注解)
jackson的功能还是很强大的,本例中实体还嵌套了List, 可以正常解析正确。
第二种方法: 使用HttpServletRequest, 然后读取流,这个方法可以按照自己的方式进行解析。
)
@RequestMapping(value = "/student", method = RequestMethod.POST) public String addStudent(@RequestBody Student record) { return "ok"; } @RequestMapping(value = "/student1", method = RequestMethod.POST) public String addStudent1(HttpServletRequest rquests) throws IOException { ServletInputStream aaa = rquests.getInputStream(); return "ok"; }
原文:https://www.cnblogs.com/liufei1983/p/9043570.html