문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
springframework:mvc:handlerexceptionresolver [2015/10/19 17:41] kwon37xi |
springframework:mvc:handlerexceptionresolver [2021/07/14 12:49] (현재) kwon37xi |
||
---|---|---|---|
줄 3: | 줄 3: | ||
* Spring MVC의 예외 처리를 담당한다. | * Spring MVC의 예외 처리를 담당한다. | ||
* [[http:// | * [[http:// | ||
+ | * [[springframework: | ||
+ | * [[springframework: | ||
===== 기본 확장 ===== | ===== 기본 확장 ===== | ||
줄 8: | 줄 10: | ||
* 이때 '' | * 이때 '' | ||
- | ===== @ControllerAdvice ===== | ||
- | * '' | ||
- | * [[http:// | ||
- | <code java> | ||
- | package javabeat.net; | ||
- | import java.io.IOException; | + | ===== 참조 |
- | import java.sql.SQLException; | + | * [[http://www.baeldung.com/exception-handling-for-rest-with-spring|Error Handling for REST with Spring]] |
- | + | ||
- | 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 | + | |
- | andView.setViewName(" | + | |
- | return andView; | + | |
- | } | + | |
- | @ExceptionHandler(SQLException.class) | + | |
- | public ModelAndView handleSQLException(SQLException exception){ | + | |
- | ModelAndView andView | + | |
- | andView.setViewName(" | + | |
- | return andView; | + | |
- | } | + | |
- | // JSON 에러 내면서 Http Status Code 변경 | + | |
- | @ExceptionHandler(Exception.class) | + | |
- | @ResponseBody | + | |
- | public ResponseEntity< | + | |
- | // org.springframework.http.HttpStatus 를 참조할 것. | + | |
- | HttpStatus httpStatus | + | |
- | SomeErrorObject errorObject | + | |
- | + | ||
- | return new ResponseEntity<> | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== 주요 Spring MVC Exception에 대해 오버라이드 하지 말 것 ===== | + | |
- | * '' | + | |
- | * [[https://doanduyhai.wordpress.com/2012/ | + | |
- | * 주요 Exception들에 대해 올바른 응답 코드가 내려가도록 처리한다. | + | |
- | * [[https:// | + | |