문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
|
springframework:mvc:controlleradvice [2018/09/06 10:39] kwon37xi |
springframework:mvc:controlleradvice [2021/07/14 12:49] (현재) kwon37xi |
||
|---|---|---|---|
| 줄 5: | 줄 5: | ||
| ===== @ExceptionHandler ===== | ===== @ExceptionHandler ===== | ||
| * [[springframework: | * [[springframework: | ||
| + | * [[springframework: | ||
| * [[https:// | * [[https:// | ||
| * '' | * '' | ||
| 줄 43: | 줄 44: | ||
| return new ResponseEntity<> | return new ResponseEntity<> | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Parameter Validation/ | ||
| + | === @Valid 에 의한 파라미터 Validation 오류 핸들러 === | ||
| + | * '' | ||
| + | * [[https:// | ||
| + | <code java> | ||
| + | @ControllerAdvice | ||
| + | public class RestErrorHandler { | ||
| + | |||
| + | private MessageSource messageSource; | ||
| + | |||
| + | @Autowired | ||
| + | public RestErrorHandler(MessageSource messageSource) { | ||
| + | this.messageSource = messageSource; | ||
| + | } | ||
| + | |||
| + | @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| + | @ResponseStatus(HttpStatus.BAD_REQUEST) | ||
| + | @ResponseBody | ||
| + | public ValidationErrorDTO processValidationError(MethodArgumentNotValidException ex) { | ||
| + | BindingResult result = ex.getBindingResult(); | ||
| + | List< | ||
| + | |||
| + | return processFieldErrors(fieldErrors); | ||
| + | } | ||
| + | |||
| + | private ValidationErrorDTO processFieldErrors(List< | ||
| + | ValidationErrorDTO dto = new ValidationErrorDTO(); | ||
| + | |||
| + | for (FieldError fieldError: fieldErrors) { | ||
| + | String localizedErrorMessage = resolveLocalizedErrorMessage(fieldError); | ||
| + | dto.addFieldError(fieldError.getField(), | ||
| + | } | ||
| + | |||
| + | return dto; | ||
| + | } | ||
| + | |||
| + | private String resolveLocalizedErrorMessage(FieldError fieldError) { | ||
| + | Locale currentLocale = LocaleContextHolder.getLocale(); | ||
| + | String localizedErrorMessage = messageSource.getMessage(fieldError, | ||
| + | |||
| + | //If the message was not found, return the most accurate field error code instead. | ||
| + | //You can remove this check if you prefer to get the default error message. | ||
| + | if (localizedErrorMessage.equals(fieldError.getDefaultMessage())) { | ||
| + | String[] fieldErrorCodes = fieldError.getCodes(); | ||
| + | localizedErrorMessage = fieldErrorCodes[0]; | ||
| + | } | ||
| + | |||
| + | return localizedErrorMessage; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | === BindingException 처리 === | ||
| + | * [[https:// | ||
| + | <code java> | ||
| + | @ExceptionHandler(BindException.class) | ||
| + | @ResponseStatus(HttpStatus.BAD_REQUEST) | ||
| + | public ExDto handleBindException(BindException e) { | ||
| + | // BindException이 BindingResult, | ||
| } | } | ||
| </ | </ | ||
| 줄 52: | 줄 114: | ||
| * [[https:// | * [[https:// | ||
| + | ===== ResponseBodyAdvice, | ||
| + | * [[https:// | ||
| + | * https:// | ||
| + | * [[https:// | ||
| + | ===== 참고 ===== | ||
| + | * [[http:// | ||