I have a pojo like below (please assume a controller and rest of the code. Application is in Spring boot):
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class User {
@NotBlank(message = "userName is blank")
private String userName;
@NotBlank(message = "secretKey is blank")
private String secretKey;
}
and defined a ExceptionHandler class annotated with @ControllerAdvice
and defined a method as bellow:
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
protected ResponseEntity<ErrorResponse> handleMethodArgNotValidException(MethodArgumentNotValidException ex,Locale locale) {
// code to handle exception.
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {WebExchangeBindException.class})
protected ResponseEntity<ErrorResponse> handleException(WebExchangeBindException ex, Locale locale) {
// code to handle exception.
}
But in this case even if both fields have validation errors client is getting only one.
I want to ask that is there any way I can list out all the validation errors in the response of this endpoint?
curl --location --request POST 'localhost/api/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"userName": null,
"secretKey": null
}'