@Controller
에서 발생한 예외를 잡아 처리해준다.@Controller
를 관리한다.@RestControllerAdvice
: 예외를 잡아 핸들링하면서, @RestController
와 같이 결과값을 JSON 으로 반환한다.https://jeong-pro.tistory.com/195
@Controller
또는 @ControllerAdvice
가 붙은 Controller 에서 내에서 발생하는 예외를 잡아 핸들링한다. → @Service 와 같은 컨트롤러 객체가 아닌 경우에는 사용 X@ExceptionHandler
에서 어떤 매개변수도 받지 않으면 전역 에러를 처리한다.@Controller
public class SimpleController {
// ...
@ExceptionHandler
public ResponseEntity<String> handle(IOException ex) {
// ...
}
}
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception; nested exception is java.lang.IllegalStateException: No exception types mapped to public org.springframework.http.ResponseEntity chess.controller.ExceptionAdvice.handleException()
@ExceptionHandler
의 속성값에 원하는 에러 클래스를 입력한다.@ExceptionHandler({FileSystemException.class, RemoteException.class})
public ResponseEntity<String> handle(IOException ex) {
// ...
}