๐ŸŒฑ Spring MVC (14) API ์š”์ฒญ์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ (@ExceptionHandler, @ControllerAdvice)

Kim Dae Hyunยท2021๋…„ 7์›” 20์ผ
0

Spring-MVC

๋ชฉ๋ก ๋ณด๊ธฐ
12/13
post-thumbnail

Github ์†Œ์Šค์ฝ”๋“œ

๐Ÿ”Ž API ์š”์ฒญ์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

API์— ๋Œ€ํ•œ ์˜ˆ์™ธ๋Š” HTMLํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์™€๋Š” ์ƒ๋‹นํžˆ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์ด์ „ ํฌ์ŠคํŠธ์—์„œ MVC์—์„œ ์—๋Ÿฌ ํŽ˜์ด์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์˜ˆ์™ธ ์ƒํ™ฉ(์—๋Ÿฌ์ฝ”๋“œ)์— ๋งž๋Š” ์˜ˆ์™ธ ํŽ˜์ด์ง€๋ฅผ ์ •์ฑ…์— ๋งž๋Š” ๊ฒฝ๋กœ, ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ ํ•ฉํ•œ ์—๋Ÿฌ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์—ˆ์ฃ .

API์˜ ๊ฒฝ์šฐ ์—๋Ÿฌ์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ HTMLํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค˜์„  ์•ˆ ๋ฉ๋‹ˆ๋‹ค.
์ผ๋‹จ API์š”์ฒญ์€ View๋ฅผ ๋žœ๋”๋ง ๋ฐ›๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ ๊ฐ€๊ณต์ฒ˜๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ธฐ ์œ„ํ•จ์ด์ฃ .
๋˜ํ•œ API์˜ ๊ฒฝ์šฐ ์—”๋“œํฌ์ธํŠธ ์‚ฌ์šฉ์ž๊ฐ€ ์กฐ๊ธˆ ๋‹ค๋ฅธ ์„ฑ๊ฒฉ์„ ๊ฐ–๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ํ•ด๋‹น API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋Š” ์ •ํ™•ํ•œ ๋ชฉ์ ์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ†ต ์›น ํŽ˜์ด์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ๋„ ๋ชฉ์ ์„ ์ง€๋‹ˆ๊ณ  ์žˆ์ง€๋งŒ ๋ณด๋‹ค ๋ฒ”์šฉ์ ์ด๋ฏ€๋กœ ์กฐ๊ธˆ ์ถ”์ƒ์ ์ผ์ง€๋ผ๋„ ์ดํ•ด ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„์˜ ์—๋Ÿฌ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ API๋Š” ํ•ด๋‹น API์˜ ์ŠคํŽ™์— ๋งž๋Š” ๋ณด๋‹ค ์ •๋ฐ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ์‘๋‹ต ๋ฐฉ์‹๋„ ๋‹น์—ฐํžˆ HTMLํŽ˜์ด์ง€๊ฐ€ ์•„๋‹Œ JSONํ˜•์‹์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ๊ฐ„๋‹จํ•œ ์˜ˆ์™ธ ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•˜๊ณ  JSONํ˜•ํƒœ๋กœ API์ŠคํŽ™์— ๋งž๋Š” ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์˜ˆ์ œ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๐Ÿ”Ž @ExceptionHandler๋ฅผ ์ด์šฉํ•œ API ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ memberId๋ฅผ ์ด์šฉํ•ด ์—ฌ๋Ÿฌ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

@Slf4j
@RestController
public class ApiExceptionController {

    @GetMapping("/api/members/{memberId}")
    public String getMember(@PathVariable String memberId) {
        if (memberId.equals("bad"))
            throw new IllegalArgumentException("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค.");
            
        if (memberId.equals("user-ex"))
            throw new UserException("์‚ฌ์šฉ์ž ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.");
            
        if (memberId.equals("runtime"))
            throw new RuntimeException("์ž˜๋ชป๋œ ์‚ฌ์šฉ์ž์ž…๋‹ˆ๋‹ค.");
            
        return "ok";
    }
}

๊ฐ€์žฅ ๋จผ์ € memberId๊ฐ€ "bad"๋กœ ์š”์ฒญ๋˜์—ˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” IllegalArgumentException์— ๋Œ€ํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

  • @ExceptionHandler๋กœ IllegalArgumentException์— ๋Œ€ํ•œ ์˜ˆ์™ธ๋ฅผ ํ•ธ๋“ค๋ง ๋ฉ”์„œ๋“œ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ @ExceptionHandler๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ์ปจํŠธ๋กค๋Ÿฌ ๋‚ด์—์„œ ํ•ด๋‹น ์˜ˆ์™ธ ๋ฐœ์ƒ์‹œ ์•„๋ž˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์™ธ ๋ฐœ์ƒ์‹œ ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ๋ชจ๋“  ๋™์ž‘์ด ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.
์ผ๋‹จ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ผ๋‹จ DispatcherServlet๊นŒ์ง€๋Š” ์˜ˆ์™ธ๋ฅผ ๋˜์ง‘๋‹ˆ๋‹ค.
์ด๋•Œ @ExceptionHandler๊ฐ€ ๋ถ™์–ด์žˆ๋‹ค๋ฉด ExceptionHandlerExceptionResolver์— ์˜ํ•ด ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๋งˆ์น˜ ์ปจํŠธ๋กค๋Ÿฌ์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌํ„ด๋˜๋Š” ํƒ€์ž…์€ ์–ผ๋งˆ๋“ ์ง€ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋˜๋Š” ์˜ˆ์™ธ๊ฐ์ฒด๊ฐ€์™€ @ExceptionHandler์˜ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๊ฐ™๋‹ค๋ฉด @ExceptionHandler์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

@ResponseStatus(HttpStatus.BAD_REQUEST) 
@ExceptionHandler(IllegalArgumentException.class)
public ErrorResult illegalArgumentExceptionHandler(IllegalArgumentException e) {
    log.info("[ExceptionHandler: IllegalArgumentException", e);
    return new ErrorResult("BAD", e.getMessage());
}
[ ErrorResult ]
@Data
@AllArgsConstructor
public class ErrorResult {

    private String code;
    private String message;
}

๋‘ ๋ฒˆ์งธ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ• ์ž…๋‹ˆ๋‹ค. UserException์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ์ž…๋‹ˆ๋‹ค.
์ด๋ฒˆ์—๋Š” @ExceptionHandler์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ƒ๋žตํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ์ด๋ฒˆ์—๋Š” ResponseEntity๋ฅผ ์‘๋‹ต๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

@ExceptionHandler
public ResponseEntity<ErrorResult> userExceptionHandler(UserException e) {
    log.info("[ExceptionHandler: UserExceptionHandler", e);
    ErrorResult errorResult = new ErrorResult("USER-EXCEPTION", e.getMessage());
    return new ResponseEntity<ErrorResult>(errorResult, HttpStatus.BAD_REQUEST);
}

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ทธ์™ธ Exception์— ๋Œ€ํ•œ ์—๋Ÿฌ ํ•ธ๋“ค๋ง ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.
์œ„ ๋‘ ๊ฐœ ์˜ˆ์™ธ๋ฅผ ๋น„๋กฏํ•ด ๋ชจ๋“  ์˜ˆ์™ธ๋Š” Exception์˜ ์ž์‹์ž…๋‹ˆ๋‹ค.
์ด์ œ๋Š” ๋ญ”๊ฐ€ ์Šคํ”„๋ง์˜ ๋Œ€์›์น™์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ..
๊ตฌ์ฒด์ ์ธ ๊ฒƒ๋ถ€ํ„ฐ ๋œ ๊ตฌ์ฒด์ ์ธ ๊ฒƒ ์ˆœ์œผ๋กœ.
(๊ตฌ์ฒด์ ์ธ ๊ฒƒ->์ž์‹ํด๋ž˜์Šค, ๋œ ๊ตฌ์ฒด์ ์ธ ๊ฒƒ -> ๋ถ€๋ชจ ํด๋ž˜์Šค)

์ž์‹ํด๋ž˜์Šค๋กœ ์ •์˜๋œ ์˜ˆ์™ธ๋Š” ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ ์ฒ˜๋ฆฌ๋˜๊ณ  ๊ทธ์™ธ ๋ชจ๋“  ์˜ˆ์™ธ๋Š” Exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
์˜ˆ์™ธ์ฝ”๋“œ๋Š” 500 ์„œ๋ฒ„ ๋‚ด๋ถ€ ์—๋Ÿฌ๋กœ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
(HttpStatus.INTERNAL_SERVER_ERROR)

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler
public ErrorResult exceptionHandler(Exception e) {
    log.info("[ExceptionHandler: Exception", e);
    ErrorResult errorResult = new ErrorResult("EX", e.getMessage());
    return errorResult;
}

์ปจํŠธ๋กค๋Ÿฌ ์ „์ฒด ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

@Slf4j
@RestController
public class ApiExceptionController {

    /**
     * ์ด ์ปจํŠธ๋กค๋Ÿฌ์—์„œ IllegalArgumentException ๋ฐœ์ƒ์‹œ ๋ฐ”๋กœ ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์€ ์•„๋‹˜
     * ์ผ๋‹จ ์˜ˆ์™ธ๋Š” DispatcherServlet๊นŒ์ง€ ์ „ํŒŒ๋˜๊ณ  DispatcherServlet์—์„œ ์ ํ•ฉํ•œ ExceptionResolver๋ฅผ ์ฐพ์Œ
     * @ExceptionHandler๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ ExceptionHandlerExceptionResolver๋ฅผ ์‚ฌ์šฉ
     * ExceptionHandlerExceptionResolver๋ฅผ๊ฐ€ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ„์„ํ•˜์—ฌ ์•Œ๋งž๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ ์ฃผ๋Š” ๊ฒƒ.
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST) // ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋กœ ์˜ˆ์™ธ๋ฅผ ์žก์•„ ์ฒ˜๋ฆฌํ•˜์—ฌ๋„ 200 OK๋กœ ๋จ (์ •์ƒํ๋ฆ„์ด๊ธฐ ๋•Œ๋ฌธ)
    @ExceptionHandler(IllegalArgumentException.class)
    public ErrorResult illegalArgumentExceptionHandler(IllegalArgumentException e) {
        log.info("[ExceptionHandler: IllegalArgumentException", e);
        return new ErrorResult("BAD", e.getMessage());
    }

    /**
     * ๋ฆฌํ„ด๊ฐ’์œผ๋กœ ResponseEntity๋ฅผ ์‚ฌ์šฉ
     */
    @ExceptionHandler
    public ResponseEntity<ErrorResult> userExceptionHandler(UserException e) {
        log.info("[ExceptionHandler: UserExceptionHandler", e);
        ErrorResult errorResult = new ErrorResult("USER-EXCEPTION", e.getMessage());
        return new ResponseEntity<ErrorResult>(errorResult, HttpStatus.BAD_REQUEST);
    }

    /**
     * Exception์„ ์ƒ์†ํ•˜๋Š” ์ž์‹ ์˜ˆ์™ธ ํด๋ž˜์Šค (๋””ํ…Œ์ผํ•œ ์˜ˆ์™ธ ํด๋ž˜์Šค)๊ฐ€ ์—†๋‹ค๋ฉด Exceptionํด๋ž˜์Šค๋ฅผ ํ•ธ๋“ค๋งํ•˜๋Š” ์•„๋ž˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ
     * @ExceptionHandler๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์€ RunTImeException์„ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด ์•„๋ž˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋  ๊ฒƒ์ž„.
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public ErrorResult exceptionHandler(Exception e) {
        log.info("[ExceptionHandler: Exception", e);
        ErrorResult errorResult = new ErrorResult("EX", e.getMessage());
        return errorResult;
    }

    @GetMapping("/api/members/{memberId}")
    public String getMember(@PathVariable String memberId) {
        if (memberId.equals("bad"))
            throw new IllegalArgumentException("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค.");
        if (memberId.equals("user-ex"))
            throw new UserException("์‚ฌ์šฉ์ž ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.");
        if (memberId.equals("runtime"))
            throw new RuntimeException("์ž˜๋ชป๋œ ์‚ฌ์šฉ์ž์ž…๋‹ˆ๋‹ค.");
        return "ok";
    }
}

๊ฐ ์˜ˆ์™ธ ๋ฐœ์ƒ ์š”์ฒญ์— ๋Œ€ํ•œ Postman ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.


๐Ÿ”Ž @ControllerAdvice๋ฅผ ์ด์šฉํ•œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ฝ”๋“œ ๋ถ„๋ฆฌ

์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฝ”๋“œ๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ๊ธธ๊ณ  ์ •์ƒ์ ์ธ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์žˆ๋Š” ๊ฒƒ์ด ๋ณ„๋กœ ๋ณด๊ธฐ ์ข‹์ง€ ์•Š์Šต๋‹ˆ๋‹ค.. ๋ถ„๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด์ „ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋งŒ ๋”ฑ ๋ณต์‚ฌํ•ด์„œ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

@RestControllerAdvice๋Š” @ControllerAdvice์— @ResponseBody๊ฐ€ ๋ถ™์€ ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
@RestController์™€ Controller์™€ ๋™์ผํ•œ ๊ด€๊ณ„๋„ค์š”.

์ด๋ ‡๊ฒŒ @ControllerAdvice๋งŒ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ •์˜ํ•œ ์˜ˆ์™ธ ํ•ธ๋“ค๋ง ๋ฉ”์„œ๋“œ๊ฐ€ ๊ธ€๋กœ๋ฒŒํ•˜๊ฒŒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ์ปจํŠธ๋กค๋Ÿฌ์— ์ ์šฉ๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

์ ์šฉ๋ฒ”์œ„๋Š” ์–ด๋…ธํ…Œ์ด์…˜ ๋‹จ์œ„, ํŒจํ‚ค์ง€ ๋‹จ์œ„, ์ƒ์†๊ด€๊ณ„ ๋‹จ์œ„ ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ์„ค์ • ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

[ ์–ด๋…ธํ…Œ์ด์…˜ ๋‹จ์œ„ ]
@ControllerAdvice(annotations = RestController.class)
public class ExceptionControllerAdvice1 {}

[ ํŒจํ‚ค์ง€ ๋‹จ์œ„ ]
@ControllerAdvice("com.package.dhk22")
public class ExceptionControllerAdvice2 {}

[ ์ƒ์†๊ด€๊ณ„ ๋‹จ์œ„ ]
@ControllerAdvice(
      assignableTypes = {ClassInterface.class,
      AbstractController.class})
public class ExceptionControllerAdvice3 {}

ControllerAdvice ์ „์ฒด ์ฝ”๋“œ

@Slf4j
@RestControllerAdvice
public class ExceptionControllerAdvice {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(IllegalArgumentException.class)
    public ErrorResult illegalArgumentExceptionHandler(IllegalArgumentException e) {
        log.info("[ExceptionHandler: IllegalArgumentException", e);
        return new ErrorResult("BAD", e.getMessage());
    }

    @ExceptionHandler
    public ResponseEntity<ErrorResult> userExceptionHandler(UserException e) {
        log.info("[ExceptionHandler: UserExceptionHandler", e);
        ErrorResult errorResult = new ErrorResult("USER-EXCEPTION", e.getMessage());
        return new ResponseEntity<ErrorResult>(errorResult, HttpStatus.BAD_REQUEST);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    public ErrorResult exceptionHandler(Exception e) {
        log.info("[ExceptionHandler: Exception", e);
        ErrorResult errorResult = new ErrorResult("EX", e.getMessage());
        return errorResult;
    }

}

์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง MVC 2ํŽธ ์„ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€