API ์์ธ ์ฒ๋ฆฌ - @ControllerAdvice
์ง๋๊ธ
https://mong-dev.tistory.com/44
API ์์ธ ์ฒ๋ฆฌ - HandlerExceptionResolver(2) : ResponseStatusExceptionResolver, DefaultHandlerExceptionResolver, ExceptionHan
https://mong-dev.tistory.com/43 API ์์ธ ์ฒ๋ฆฌ - HandlerExceptionResolver(1) ์คํ๋ง ๋ถํธ์ ์์ธ ์ฒ๋ฆฌ ์คํ๋ง ๋ถํธ์ ๊ธฐ๋ณธ ์ค์ ์ ์ค๋ฅ ๋ฐ์์ /error ๋ฅผ ์ค๋ฅ ํ์ด์ง๋ก ์์ฒญํ๋ค. BasicErrorController ๋..
mong-dev.tistory.com
@ControllerAdvice
์ด์ ๊ธ์์ ๋๋์ด @ExceptionHandler ๋ฅผ ์ฌ์ฉํด์ ์์ธ๋ฅผ ๊น๋ํ๊ฒ ์ฒ๋ฆฌํ ์ ์๊ฒ ๋์์ง๋ง, ์ ์ ์ฝ๋์ ์์ธ ์ฒ๋ฆฌ ์ฝ๋๊ฐ ํ๋์ ์ปจํธ๋กค๋ฌ์ ์์ฌ ์์๋ค. @ControllerAdvice ๋๋ @RestControllerAdvice ๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ ๋ถ๋ฆฌํ ์ ์๋ค.
์ฐธ๊ณ ๋ก @RestControllerAdvice ๋ @ControllerAdvice + @Responsebody ๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
์ด์ ๊ธ์ ์์ฑํ๋ ์์ธ์ฒ๋ฆฌ๋ถ๋ถ๋ค์ ExControllerAdvice ํด๋์ค๋ก ๋ฐ๋ก ๋ถ๋ฆฌ๋ฅผ ์์ผ๋ณด์.
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
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", "๋ด๋ถ ์ค๋ฅ");
}
}
์ด๋ ๊ฒ ์ฎ๊ฒจ์ ๋ค์ ์คํ์ ํด๋ณด๋ฉด, ๊ฒฐ๊ณผ๊ฐ์ ์ด์ ๊ณผ ๋์ผํ๊ฒ ๋จ๋ ๊ฒ์ ์ ์ ์๋ค.
๊ฒฐ๊ตญ ์ด ํด๋์ค ํ๋์ ์์ธ์ฒ๋ฆฌ๋ฅผ ๋ชจ์์ ์ฒ๋ฆฌํ ์ ์๊ฒ ๋์๋ค. ์ฌ์ฉ๋ฐฉ๋ฒ์ ์๋นํ ๊ฐ๋จํ๋ค.
์ ๋ฆฌ
- @ControllerAdvice๋ ๋์์ผ๋ก ์ง์ ํ ์ฌ๋ฌ ์ปจํธ๋กค๋ฌ์ @ExceptionHandler, @InitBinder๊ธฐ๋ฅ์ ๋ถ์ฌํด์ฃผ๋ ์ญํ ์ ํ๋ค.
- @ControllerAdvice ์ ๋์์ ์ง์ ํ์ง ์์ผ๋ฉด ๋ชจ๋ ์ปจํธ๋กค๋ฌ์ ์ ์ฉ๋๋ค. (๊ธ๋ก๋ฒ ์ ์ฉ)
- @RestControllerAdvice ๋ @ControllerAdvice ์ ๊ฐ๊ณ , @ResponseBody ๊ฐ ์ถ๊ฐ๋์ด ์๋ค.
(@Controller ,@RestController ์ ์ฐจ์ด์ ๊ฐ๋ค.)
๋ง์ง๋ง์ผ๋ก ๋์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ง์ ํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ๋ฐฉ๋ฒ์ ์์๋ณด์.
(์คํ๋ง ๊ณต์๋ฌธ์ ์ฐธ๊ณ : https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann- controller-advice)
// 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 {}
์คํ๋ง ๊ณต์ ๋ฌธ์ ์์ ์์ ๋ณด๋ ๊ฒ ์ฒ๋ผ ํน์ ์ ๋ ธํ ์ด์ ์ด ์๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ง์ ํ ์ ์๊ณ , ํน์ ํจํค์ง๋ฅผ ์ง์ ์ง์ ํ ์๋ ์๋ค. ํจํค์ง ์ง์ ์ ๊ฒฝ์ฐ ํด๋น ํจํค์ง์ ๊ทธ ํ์์ ์๋ ์ปจํธ๋กค๋ฌ๊ฐ ๋์์ด ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํน์ ํด๋์ค๋ฅผ ์ง์ ํ ์๋ ์๋ค. ๋์ ์ปจํธ๋กค๋ฌ ์ง์ ์ ์๋ตํ๋ฉด ๋ชจ๋ ์ปจํธ๋กค๋ฌ์ ์ ์ฉ๋๋ค.
์ด์ @ExceptionHandler ์ @ControllerAdvice ๋ฅผ ์กฐํฉํ๋ฉด ์์ธ๋ฅผ ๊น๋ํ๊ฒ ํด๊ฒฐํ ์ ์๋ค.
๊ทธ๋ผ ์ด์ ์ผ๋ฅธ ํ๋ก์ ํธ ์ฝ๋ ๋ฆฌํฉํ ๋งํ๋ฌ ๊ฐ์ผ๊ฒ ๋ค.... :)