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", "내부 오류"); + } +}