Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过curlopostman测试Spring引导一对多jpapost请求_Java_Json_Spring Boot_Postman - Fatal编程技术网

Java 如何通过curlopostman测试Spring引导一对多jpapost请求

Java 如何通过curlopostman测试Spring引导一对多jpapost请求,java,json,spring-boot,postman,Java,Json,Spring Boot,Postman,在我的spring boot应用程序中,我有以下型号:- @Entity @Table(name = "STUDENT") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column private String name; @Column private int mo

在我的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 ..";
        }
    }
}
我得到了以下错误:-

决心[org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:无法反序列化
java.util.ArrayList
out-START\u对象标记的实例;嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化
java.util.ArrayList
out-START\u对象标记的实例 在[源:(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"}

我认为我的应用程序工作正常。如何通过邮递员或CURL发出正确的JSON请求?

你不应该在帖子正文中添加
学生
使用如下

 [
  {
    "name": "masi",
    "mobile": 12345,
    "department": {
      "field":"value"
    }
  },
  {
    "name": "masi2",
    "mobile": 1234500,
     "department": {
      "field":"value"
    }
  }
]

我不知道系课的字段,请进行相应编辑

您不应在帖子正文中添加学生 使用如下

 [
  {
    "name": "masi",
    "mobile": 12345,
    "department": {
      "field":"value"
    }
  },
  {
    "name": "masi2",
    "mobile": 1234500,
     "department": {
      "field":"value"
    }
  }
]

我不知道部门类的字段,请进行相应编辑

你能分享你的控制器代码吗,我想看看你是如何接受帖子正文的?我的问题中有StudentController。我想测试
@PostMapping(“/saveStudent/{deptName}”)公共字符串saveStudent(@RequestBody List studentList,@PathVariable String deptName){
controller。另外,我添加了邮递员控制台日志。你能分享你的控制器代码吗,我想看看你是如何使用它来接受邮件正文的?我的问题中有StudentController。我想测试
@PostMapping(/saveStudent/{deptName})公共字符串saveStudent(@RequestBody List studentList,@PathVariable String deptName){
controller。此外,我还添加了邮递员控制台日志。当我尝试此操作时,我发现此错误:-已解决[org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:无法从字符串“hr”反序列化类型
java.lang.Long
的值:不是有效的长值;嵌套异常是com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“hr”反序列化类型
java.lang.Long
的值:在[Source:(PushbackInputStream);行:5,列:19](通过引用链:java.util.ArrayList[0]->com.example.onetomany.model.Student[“department”])处不是有效的长值,在上述错误之后:-我厌倦了这个
[{“name”:“masi”,“mobile”:12345,“department”:“1”},{“name”:“masi2”,“mobile”:1234500,“部门”:“1”}]
然后出现错误:.w.s.m.s.DefaultHandlerExceptionResolver:已解决[org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:未解析的正向引用:;嵌套异常为com.fasterxml.jackson.databind.desr.unsolvedForwardReference:未解析的正向引用:位于[Source:(PushbackInputStream);第12行,第1列]对象id[1](对于
com.example.onetomany.model.Department
)位于[Source:(PushbackInputStream);行:5,列:22],对象id[1](对于
com.example.onetomany.model.Department
)位于[Source:(PushbackInputStream);department是一个对象,就像我为学生所做的那样,您需要对department进行同样的操作。我更新了答案,请立即检查,并在我尝试此操作时编辑side department中的字段值。我发现此错误:-已解决[org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:无法从字符串“hr”反序列化类型
java.lang.Long
的值:不是有效的长值;嵌套异常是com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“hr”反序列化类型
java.lang.Long
的值:在[Source:(PushbackInputStream);行:5,列:19](通过引用链:java.util.ArrayList[0]->com.example.onetomany.model.Student[“department”])处不是有效的长值,在上述错误之后:-我厌倦了这个
[{“name”:“masi”,“mobile”:12345,“department”:“1”},{“name”:“masi2”,“mobile”:1234500,“部门”:“1”}]
然后出现错误:.w.s.m.s.DefaultHandlerExceptionResolver:已解决[org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:未解析的正向引用:;嵌套异常为com.fasterxml.jackson.databind.desr.unsolvedForwardReference:未解析的正向引用:位于[Source:(PushbackInputStream);第12行,第1列]对象id[1](对于
com.example.onetomany.model.Department
)位于[Source:(PushbackInputStream);行:5,列:22],对象id[1](对于
com.example.onetomany.model.Department
)位于[Source:(PushbackInputStream);部门是一个对象,就像我为学生所做的一样,你也需要这样做。我更新了答案,请现在检查并编辑部门中的字段值