[spring-projects/spring-boot]springboot 2.2.5.RELEASE POST请求入参序列化的问题

2023-12-06 399 views
1

jdk: jdk1.8 springBoot:V2.2.5.RELEASE 请求方式:POST

POST接口: @PostMapping("") public String test(@RequestBody School a){ log.info("------------------"); log.info(JSONUtil.toJsonStr(a)); return "success"; }

public class School {

private List<Student> studentList;

public List<Student> getStudentList() {
    return studentList;
}

public void setStudentList(List<Student> studentList) {
    this.studentList = studentList;
}

}

public class Student {

private List<Teacher> teacherList;

private List<String> testList;

public List<Teacher> getTeacherList() {
    return teacherList;
}

public void setTeacherList(List<Teacher> teacherList) {
    this.teacherList = teacherList;
}

public List<String> getTestList() {
    return testList;
}

public void setTestList(List<String> testList) {
    this.testList = testList;
}

}

public class Teacher {

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

请求入参: { "studentList": [ { "teacherList": [ { "name": ["ds"] } ], "testList":["d"] } ] } 输出:{"studentList":[{"teacherList":[{}]}]}

请求入参: { "studentList": [ { "teacherList": [ { "name": [ "ds", "1" ] } ], "testList": [ "d" ] } ] } 系统异常:Invalid JSON input: Cannot construct instance of

问题: 为什么入参的name属性为数组时,且size=1能够正常序列化,testList为null, 当size>1时,报序列化错误

回答

7

Spring Boot2.2.x不再受支持,因此您需要首先升级到至少 Spring Boot 2.7。如果您仍然遇到问题并且需要我们的支持,一旦您分享了我们可以自行运行的小样本,我们就可以重新打开此问题。请不要将代码粘贴到类似的文本中。