I have a simple spring
webservice that offers a @PostMapping
and takes a json array of elements.
I want spring to automatically validate each element in the list.
@RestController
public class PersonServlet {
@PostMapping
public void insertPersons(@RequestBody @Valid List<PersonDto> array) {
}
}
public class PersonDto {
@NotBlank
private String firstname;
@NotBlank
private String lastname;
}
The following POST
request should fail with a validation error that firstname
is missing:
[
{
"lastname": "john"
},
{
"firstname": "jane",
"lastname": "doe"
}
]
Result: the request is NOT rejected. Why?
Sidenote: if I just use PersonDto
as parameter (not a list), and send a json
post request with only one persons, the validation works and correctly rejects the request.
So in general the validation annotations seem to work, but just not when inside the collection!