'How To Pass Two @RequestBody from Postman (springboot)

My Controller: enter image description here

postman request body is:

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

Error: [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type Course from Array value (token JsonToken.START_ARRAY); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type Course from Array value (token JsonToken.START_ARRAY) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream);



Solution 1:[1]

@RequestBody annotated parameter is expected to hold the entire body of the request and bind to one object.

What you can do is make a new model such as EnrollmentObject and have the ID of the course and the ID of the student in there. Then, you can have @RequestBody EnrollmentObject enrollmentObject.

You should obviously go with another model name, but you get the point! ;-)

Solution 2:[2]

You can define your request body as an array like the following:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SD33N
Solution 2 blacktide