Spring Boot
Validation 예외 처리
최-코드
2024. 10. 30. 10:08
유효성 검사시에
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
ExceptionDto body = ExceptionDto.of(7, "validation error");
return this.handleExceptionInternal(body, HttpStatus.BAD_REQUEST);
}
이와 같이 예외처리를 해주면 된다.
이 때 ex.getBindingResult().getAllErrors를 통해 유효성 검사시에 발생한 모든 에러를 가져올 수 있다.