【发布时间】:2020-11-04 18:02:21
【问题描述】:
在我的 spring boot 应用程序中,我有以下模型:-
@Entity
@Table(name = "STUDENT")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column
private int mobile;
public Student() {
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "DEPT_ID", nullable = false)
private Department department;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMobile() {
return mobile;
}
public void setMobile(int mobile) {
this.mobile = mobile;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", mobile=" + mobile +
", department=" + department +
'}';
}
}
@Entity
@Table(name = "DEPARTMENT")
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
public Department() {
}
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToMany(mappedBy = "department", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Student> studentList = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
}
@RestController
public class StudentController {
@Autowired
StudentService studentService;
@Autowired
DepartmentService departmentService;
@RequestMapping(value = "/studentList", method = RequestMethod.GET)
public @ResponseBody
List<Student> getStudents(){
return studentService.getStudents();
}
@PostMapping("/saveStudent/{deptName}")
public String saveStudent(@RequestBody List<Student> studentList, @PathVariable String deptName){
try {
Department dept = departmentService.findDepartment(deptName.toUpperCase());
for(Student student: studentList)
student.setDepartment(dept);
studentService.saveStudent(studentList);
return "Student saved successfully..";
}catch (Exception ex){
ex.printStackTrace();
return "Error in saving Student ..";
}
}
}
对于上述应用程序。我想测试它的后控制器localhost:8080 /saveStudent/hr
请注意,我已经保存了人力资源部门。我厌倦了 Postman 的 JSON:-
{
"student": [{ "name": "masi",
"mobile": 12345,
"department": "hr"
}, { "name": "masi2",
"mobile": 1234500,
"department": "hr"
}]
}
我收到以下错误:-
已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_OBJECT 令牌中反序列化
java.util.ArrayList<com.example.onetomany.model.Student>的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize ofjava.util.ArrayList<com.example.onetomany.model.Student>out of START_OBJECT token 在 [来源:(PushbackInputStream);行:1,列:1]]
邮递员控制台日志:-
POST http://localhost:8080 /saveStudent/hr
400
183 ms
Network
Request Headers
Content-Type: application/json
User-Agent: PostmanRuntime/7.26.1
Accept: */*
Cache-Control: no-cache
Postman-Token: 57071c5d-e416-4b54-870a-9f318fee7166
Host: localhost:8080
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 160
Request Body
Response Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 15 Jul 2020 09:05:43 GMT
Connection: close
Response Body
{"timestamp":"2020-07-15T09:05:43.864+00:00","status":400,"error":"Bad Request","message":"","path":"/saveStudent/hr"}
我认为我的应用程序运行正常。如何通过 Postman 或 CURL 发出正确的 JSON 请求?
【问题讨论】:
-
你能分享你的控制器代码吗,我想看看你是如何接受帖子正文的?
-
我的问题中有 StudentController。我想测试
@PostMapping("/saveStudent/{deptName}") public String saveStudent(@RequestBody List<Student> studentList, @PathVariable String deptName){控制器。另外,我添加了邮递员控制台日志。
标签: java json spring-boot postman