From 526b553777b051bb8e80dffa1c0d1e08f8ef99a9 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Mon, 26 Jul 2021 21:01:01 +0900 Subject: [PATCH] exception : spring - @ControllerAdvice --- .../api/ApiExceptionV2Controller.java | 28 ++----------- .../exhandler/advice/ExControllerAdvice.java | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 exception/src/main/java/hello/exception/exhandler/advice/ExControllerAdvice.java diff --git a/exception/src/main/java/hello/exception/api/ApiExceptionV2Controller.java b/exception/src/main/java/hello/exception/api/ApiExceptionV2Controller.java index 867a8995..298e38a2 100644 --- a/exception/src/main/java/hello/exception/api/ApiExceptionV2Controller.java +++ b/exception/src/main/java/hello/exception/api/ApiExceptionV2Controller.java @@ -1,39 +1,17 @@ package hello.exception.api; import hello.exception.exception.UserException; -import hello.exception.exhandler.ErrorResult; import lombok.AllArgsConstructor; import lombok.Data; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController public class ApiExceptionV2Controller { - @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 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", "내부 오류"); - } - @GetMapping("/api2/members/{id}") public MemberDto getMember(@PathVariable("id") String id) { diff --git a/exception/src/main/java/hello/exception/exhandler/advice/ExControllerAdvice.java b/exception/src/main/java/hello/exception/exhandler/advice/ExControllerAdvice.java new file mode 100644 index 00000000..c9ca73ff --- /dev/null +++ b/exception/src/main/java/hello/exception/exhandler/advice/ExControllerAdvice.java @@ -0,0 +1,40 @@ +package hello.exception.exhandler.advice; + +import hello.exception.api.ApiExceptionController; +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.RestController; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@Slf4j +//@RestControllerAdvice(annotations = RestController.class) +@RestControllerAdvice("hello.exception.api") +//@RestControllerAdvice(assignableTypes = {ApiExceptionController.class}) +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 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", "내부 오류"); + } +}