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);
CodePudding user response:
@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! ;-)
CodePudding user response:
As you are passing two object in enrollToCourse method. So, you have to pass that object in below manner in postman.
for course object
{
"id": "PG"
}
for student object
{
"id": "123456"
}
And if you want to store the value of both course and student class in single class then make DTO of it.
CodePudding user response:
You can define your request body as an array like the following:
public ResponseEntity<String> enroll(@RequestBody Course[] courses) {
// use courses array here
}