![](/img/trans.png)
[英]Testing Spring Boot Backend Post Request using Postman and Curl
[英]How to test Spring boot one to many JPA post request by curl o postman
在我的 spring 启动应用程序中,我有以下型号:-
@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 ..";
}
}
}
对于上述应用程序。 我想测试它的帖子 controller localhost:8080 /saveStudent/hr
请注意,我已经保存了人力资源部门。 我厌倦了通过 Postman 跟随 JSON:-
{
"student": [{ "name": "masi",
"mobile": 12345,
"department": "hr"
}, { "name": "masi2",
"mobile": 1234500,
"department": "hr"
}]
}
我收到以下错误:-
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of
java.util.ArrayList<com.example.onetomany.model.Student>
out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofjava.util.ArrayList<com.example.onetomany.model.Student>
out of START_OBJECT token at [Source: (PushbackInputStream); 行:1,列:1]]
Postman 控制台日志:-
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 请求?
你不应该在帖子正文中添加student
使用如下
[
{
"name": "masi",
"mobile": 12345,
"department": {
"field":"value"
}
},
{
"name": "masi2",
"mobile": 1234500,
"department": {
"field":"value"
}
}
]
我不知道部门class的领域请相应编辑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.