ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • API ์˜ˆ์™ธ ์ฒ˜๋ฆฌ - @ControllerAdvice
    ๊ฐœ๋ฐœ/Spring Boot 2022. 7. 8. 15:14

     

     

     

    ์ง€๋‚œ๊ธ€

    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 ๋ฅผ ์กฐํ•ฉํ•˜๋ฉด ์˜ˆ์™ธ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

    ๊ทธ๋Ÿผ ์ด์ œ ์–ผ๋ฅธ ํ”„๋กœ์ ํŠธ ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋งํ•˜๋Ÿฌ ๊ฐ€์•ผ๊ฒ ๋‹ค.... :) 

     

     

    ๋Œ“๊ธ€

Designed by Tistory.