반응형
@ControllerAdvice @RestControllerAdvice
프로젝트내 모든 Exception이 발생하면 거쳐간다.
@ExceptionHandler
컨트롤러에 어떠한 요청이 있을때 모든 예외를 Catch 하여 처리를 해주는 기능이다.
전역으로 사용할 Exception Class 생성 - @RestControllerAdvice
@Advice 어노테이션이 붙은 클래스는, Exception이 나는 모든경우에 거쳐간다.
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ResponseEntity exception(Exception e){
System.out.println(" " + e.getLocalizedMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getLocalizedMessage());
}
}
- REST API에 맞게, @RestControllerAdvice 를 사용했고, @ExceptionHandler 를 적용하여 Exception을 받을 객체라는것을 명시했다
- 함수 인자로 Exception을 받아 해당하는 예외를 처리할 수 있도록 했다.
반응형
'IT > Spring Boot' 카테고리의 다른 글
[paging] 페이징 처리 구현하기 (spring boot, mybatis, react-pagination) (2) | 2023.02.16 |
---|