티스토리 뷰
dev_공부일지/spring boot + intelliJ
@ControllerAdvice 와 @RestControllerAdvice로 깔끔한 예외처리
dev_0hoon 2023. 12. 16. 15:53@ControllerAdvice
@ControllerAdvice 는 대상으로 지정한 여러 컨트롤러에 @ExceptionHandler , @InitBinder 기능을 부여해주는 역할을 한다.
@ControllerAdvice 에 대상을 지정하지 않으면 모든 컨트롤러에 적용된다. (글로벌 적용)
@RestControllerAdvice 는 @ControllerAdvice 와 같고, @ResponseBody 가 추가되어 있다. @Controller , @RestController 의 차이와 같다.
이전 글에서는 특정 컨트롤러에서 @ExceptionHandler 어노테이션으로 예외를 처리를 했을 경우에
해당 컨트롤러의 안의 맵핑 된 주소가 있을 경우에만 예외처리가 되었다.
하지만 @ControllerAdvice 또는 @RestControllerAdvice를 어노테이션을 클래스 자체에 붙여 사용 할 경우, 모든 컨트롤러의 예외가 처리 된다.
package hello.exception.exhandler.advice;
import hello.exception.exception.UserException;
import hello.exception.exhandler.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ExControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
public ErrorResult illegalExHandler(IllegalArgumentException e){
log.error("[exceptionHandler] ex",e);
return new ErrorResult("BAD", e.getMessage());
}
@ExceptionHandler //매개변수로 Exception을 두면 같은 것으로 인식하며 생략할 수 있다.
public ResponseEntity<ErrorResult> userExHandler(UserException e){
log.error("[exceptionHandler] ex", e);
ErrorResult errorResult = new ErrorResult("User-EX", e.getMessage());
return new ResponseEntity(errorResult, HttpStatus.BAD_REQUEST);
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler
public ErrorResult exHandler(Exception e){
log.error("[exceptionHandler] ex", e);
return new ErrorResult("EX", "내부오류");
}
}
이렇게 사용하면 다른 컨트롤러를 호출해서 예외가 터져도 아래처럼 처리가 된다.
하지만 모든 맵핑주소에 같은 예외처리를 하고 싶지 않을 수 있다. 이럴 때에는 지정해 줄 수가 있다.
// Target all Controllers annotated with @RestController
@ControllerAdvice(annotations = RestController.class)
public class ExampleAdvice1 {}
// Target all Controllers within specific packages
@ControllerAdvice("org.example.controllers")
public class ExampleAdvice2 {}
// Target all Controllers assignable to specific classes
@ControllerAdvice(assignableTypes = {ControllerInterface.class,
AbstractController.class})
public class ExampleAdvice3 {}
이렇게 1. RestController만(@RestController) 지정도 가능 2. 패키지 단위로 가능하며 3. 직접 여러개의 컨트롤러로도(부모) 가능하다.
package hello.exception.exhandler.advice;
import hello.exception.exception.UserException;
import hello.exception.exhandler.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice(basePackages = "hello.exception.api")
public class ExControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
public ErrorResult illegalExHandler(IllegalArgumentException e){
log.error("[exceptionHandler] ex",e);
return new ErrorResult("BAD", e.getMessage());
}
@ExceptionHandler //매개변수로 Exception을 두면 같은 것으로 인식하며 생략할 수 있다.
public ResponseEntity<ErrorResult> userExHandler(UserException e){
log.error("[exceptionHandler] ex", e);
ErrorResult errorResult = new ErrorResult("User-EX", e.getMessage());
return new ResponseEntity(errorResult, HttpStatus.BAD_REQUEST);
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler
public ErrorResult exHandler(Exception e){
log.error("[exceptionHandler] ex", e);
return new ErrorResult("EX", "내부오류");
}
}
간단히 패키지만 지정해 봤다.
'dev_공부일지 > spring boot + intelliJ' 카테고리의 다른 글
Converter(2) Convertion Service (0) | 2023.12.18 |
---|---|
Converter사용기 (1) (0) | 2023.12.18 |
@ExceptionHandler Springboot의 자동 예외처리 (0) | 2023.12.13 |
예외 자동변환하는 DefaultHandlerExceptionResolver (0) | 2023.12.13 |
예외 자동변환하는 DefaultHandlerExceptionResolver (0) | 2023.12.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 스프링공부
- 향해플러스
- 향해플러스백엔드
- thymleaf
- Java
- 스프링부트
- SpringBoot
- hypertexttransferprotocol
- reject
- 항해99
- ArgumentResolver
- 향해99
- react실행
- 리터럴
- 백엔드 개발자 공부
- rejectValue
- 항해플러스
- HTTP
- 컨트
- exception
- 로그인
- 백엔드 개발자 역량
- 예외처리
- filter
- JPA
- React
- 인터셉터
- jpa api
- Intercepter
- BindingResult
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함