사용자 도구

사이트 도구


springframework:mvc:controlleradvice

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
springframework:mvc:controlleradvice [2018/09/06 10:39]
kwon37xi
springframework:mvc:controlleradvice [2021/07/14 12:49] (현재)
kwon37xi
줄 5: 줄 5:
 ===== @ExceptionHandler ===== ===== @ExceptionHandler =====
   * [[springframework:mvc:handlerexceptionresolver|HandlerExceptionResolver]]   * [[springframework:mvc:handlerexceptionresolver|HandlerExceptionResolver]]
 +  * [[springframework:problem_spring_web|Problem Spring Web]]
   * [[https://dzone.com/articles/global-exception-handling-with-controlleradvice|Global Exception Handling With @ControllerAdvice - DZone Java]]   * [[https://dzone.com/articles/global-exception-handling-with-controlleradvice|Global Exception Handling With @ControllerAdvice - DZone Java]]
   * ''@ControllerAdvice''에 각종 예외 처리 핸들러(''@ExceptionHandler'')를 미리 등록해 두고 component-scan을 하면 자동 에러 처리가 된다.   * ''@ControllerAdvice''에 각종 예외 처리 핸들러(''@ExceptionHandler'')를 미리 등록해 두고 component-scan을 하면 자동 에러 처리가 된다.
줄 43: 줄 44:
         return new ResponseEntity<>(errorObject, httpStatus);         return new ResponseEntity<>(errorObject, httpStatus);
     }     }
 +}
 +</code>
 +
 +==== Parameter Validation/Binding Exception 처리 ====
 +=== @Valid 에 의한 파라미터 Validation 오류 핸들러 ===
 +  * ''@Valid'' Validation 시에 오류가 발생했을 때의 처리 핸들러
 +  * [[https://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-adding-validation-to-a-rest-api/|Spring From the Trenches: Adding Validation to a REST API]] 참조.
 +<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<FieldError> fieldErrors = result.getFieldErrors();
 + 
 +        return processFieldErrors(fieldErrors);
 +    }
 + 
 +    private ValidationErrorDTO processFieldErrors(List<FieldError> fieldErrors) {
 +        ValidationErrorDTO dto = new ValidationErrorDTO();
 + 
 +        for (FieldError fieldError: fieldErrors) {
 +            String localizedErrorMessage = resolveLocalizedErrorMessage(fieldError);
 +            dto.addFieldError(fieldError.getField(), localizedErrorMessage);
 +        }
 + 
 +        return dto;
 +    }
 + 
 +    private String resolveLocalizedErrorMessage(FieldError fieldError) {
 +        Locale currentLocale =  LocaleContextHolder.getLocale();
 +        String localizedErrorMessage = messageSource.getMessage(fieldError, currentLocale);
 + 
 +        //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;
 +    }
 +}
 +</code>
 +=== BindingException 처리 ===
 +  * [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/BindException.html|BindException]] 은 Controller에서 쿼리 파라미터를 객체로 변환할 때 타입이 다르던가 날짜 포맷팅이 잘못됐던가 할 때 발생한다.
 +<code java>
 +@ExceptionHandler(BindException.class)
 +@ResponseStatus(HttpStatus.BAD_REQUEST)
 +public ExDto handleBindException(BindException e) {
 +  // BindException이 BindingResult, Errors를 상속하고 있으므로 해당 데이터를 꺼내서 사용하면 된다.
 } }
 </code> </code>
줄 52: 줄 114:
   * [[https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html|DefaultHandlerExceptionResolver]]를 보면 각 예외별 응답 코드 기본값을 알 수 있다.   * [[https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html|DefaultHandlerExceptionResolver]]를 보면 각 예외별 응답 코드 기본값을 알 수 있다.
  
 +===== ResponseBodyAdvice, RequestBodyAdvice =====
 +  * [[https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html|ResponseBodyAdvice (Spring Framework 5.1.3.RELEASE API)]]
 +  * https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/RequestBodyAdvice.html
 +  * [[https://chung-develop.tistory.com/61|[Spring]스프링에서 공통 Response처리 하기(@ControllerAdvice 이용)]]
  
 +===== 참고 =====
 +  * [[http://wonwoo.ml/index.php/post/2208|Spring 의 @ControllerAdvice]]
springframework/mvc/controlleradvice.1536197957.txt.gz · 마지막으로 수정됨: 2018/09/06 10:39 저자 kwon37xi