如何从邮递员(springboot)传递两个@RequestBody

How To Pass Two @RequestBody from Postman (springboot)

我的控制器: enter image description here

postman 请求正文是:

[
    {
        "id": "PG"
    },
    {
        "id": "123456"
    }
]

错误:[org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从数组值(标记 JsonToken.START_ARRAY)中反序列化 Course 类型的值;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 [Source: (org.springframework.util.StreamUtils$NonClosingInputStream);

的数组值(标记 JsonToken.START_ARRAY)中反序列化 Course 类型的值

@RequestBody 带注释的参数应包含请求的整个主体并绑定到一个对象。

您可以做的是创建一个新模型,例如 EnrollmentObject,并在其中包含课程 ID 和学生 ID。然后,你可以有 @RequestBody EnrollmentObject enrollmentObject.

您显然应该使用其他型号名称,但您明白了! ;-)

您可以将请求正文定义为如下数组:

public ResponseEntity<String> enroll(@RequestBody Course[] courses) {
    // use courses array here
}