Created
February 1, 2023 20:30
-
-
Save ddanielsantos/bc7885d65ca4621e7315e58c22cbec27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ControllerAdvice | |
class ValidationHandler extends ResponseEntityExceptionHandler { | |
@Override | |
@ResponseBody | |
protected ResponseEntity<Object> handleMethodArgumentNotValid( | |
MethodArgumentNotValidException ex, | |
HttpHeaders headers, | |
HttpStatus status, | |
WebRequest request | |
) { | |
HashMap<String, List<String>> body = new HashMap<>(); | |
List<String> errors = ex | |
.getBindingResult() | |
.getFieldErrors() | |
.stream() | |
.map(DefaultMessageSourceResolvable::getDefaultMessage) | |
.collect(Collectors.toList()); | |
body.put("errors", errors); | |
return handleExceptionInternal(ex, body, headers, HttpStatus.BAD_REQUEST, request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment