문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
|
springframework:mvc:controlleradvice [2018/09/06 10:34] kwon37xi 만듦 |
springframework:mvc:controlleradvice [2021/07/14 12:49] (현재) kwon37xi |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| ====== @ControllerAdvice ====== | ====== @ControllerAdvice ====== | ||
| * [[https:// | * [[https:// | ||
| + | * '' | ||
| + | |||
| + | ===== @ExceptionHandler ===== | ||
| * [[springframework: | * [[springframework: | ||
| + | * [[springframework: | ||
| + | * [[https:// | ||
| + | * '' | ||
| + | * [[http:// | ||
| + | <code java> | ||
| + | package javabeat.net; | ||
| + | |||
| + | import java.io.IOException; | ||
| + | import java.sql.SQLException; | ||
| + | |||
| + | import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| + | import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| + | import org.springframework.web.servlet.ModelAndView; | ||
| + | import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
| + | |||
| + | @ControllerAdvice | ||
| + | public class ControllerAdviceTest { | ||
| + | @ExceptionHandler(IOException.class) | ||
| + | public ModelAndView handleIOException(IOException exception){ | ||
| + | ModelAndView andView = new ModelAndView(); | ||
| + | andView.setViewName(" | ||
| + | return andView; | ||
| + | } | ||
| + | @ExceptionHandler(SQLException.class) | ||
| + | public ModelAndView handleSQLException(SQLException exception){ | ||
| + | ModelAndView andView = new ModelAndView(); | ||
| + | andView.setViewName(" | ||
| + | return andView; | ||
| + | } | ||
| + | // JSON 에러 내면서 Http Status Code 변경 | ||
| + | @ExceptionHandler(Exception.class) | ||
| + | @ResponseBody | ||
| + | public ResponseEntity< | ||
| + | // org.springframework.http.HttpStatus 를 참조할 것. | ||
| + | HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; | ||
| + | SomeErrorObject errorObject = new SomeErrorObject(); | ||
| + | |||
| + | 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, | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== 주요 Spring MVC Exception에 대해 오버라이드 하지 말 것 ==== | ||
| + | * '' | ||
| + | * [[https:// | ||
| + | * 주요 Exception들에 대해 올바른 응답 코드가 내려가도록 처리한다. | ||
| + | * [[https:// | ||
| + | |||
| + | ===== ResponseBodyAdvice, | ||
| + | * [[https:// | ||
| + | * https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ===== 참고 ===== | ||
| + | * [[http:// | ||