From f17ec807b59a68d63b1526346fe82092bc354306 Mon Sep 17 00:00:00 2001 From: kimyonghwa Date: Sat, 13 Apr 2019 00:40:28 +0900 Subject: [PATCH] =?UTF-8?q?Spring=20Boot2=20(6)=20=E2=80=93=20Spring=20Exc?= =?UTF-8?q?eption=20Handling=20with=20ControllerAdvice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rest/api/advice/ExceptionAdvice.java | 31 +++++++++++++++++++ .../exception/CUserNotFoundException.java | 15 +++++++++ .../api/controller/v1/UserController.java | 4 ++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/rest/api/advice/ExceptionAdvice.java create mode 100644 src/main/java/com/rest/api/advice/exception/CUserNotFoundException.java diff --git a/src/main/java/com/rest/api/advice/ExceptionAdvice.java b/src/main/java/com/rest/api/advice/ExceptionAdvice.java new file mode 100644 index 0000000..56ce6a5 --- /dev/null +++ b/src/main/java/com/rest/api/advice/ExceptionAdvice.java @@ -0,0 +1,31 @@ +package com.rest.api.advice; + +import com.rest.api.advice.exception.CUserNotFoundException; +import com.rest.api.model.response.CommonResult; +import com.rest.api.service.ResponseService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.servlet.http.HttpServletRequest; + +@RequiredArgsConstructor +@RestControllerAdvice +public class ExceptionAdvice { + + private final ResponseService responseService; + +// @ExceptionHandler(Exception.class) +// @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) +// protected CommonResult defaultException(HttpServletRequest request, Exception e) { +// return responseService.getFailResult(); +// } + + @ExceptionHandler(CUserNotFoundException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + protected CommonResult userNotFoundException(HttpServletRequest request, CUserNotFoundException e) { + return responseService.getFailResult(); + } +} diff --git a/src/main/java/com/rest/api/advice/exception/CUserNotFoundException.java b/src/main/java/com/rest/api/advice/exception/CUserNotFoundException.java new file mode 100644 index 0000000..c91e367 --- /dev/null +++ b/src/main/java/com/rest/api/advice/exception/CUserNotFoundException.java @@ -0,0 +1,15 @@ +package com.rest.api.advice.exception; + +public class CUserNotFoundException extends RuntimeException { + public CUserNotFoundException(String msg, Throwable t) { + super(msg, t); + } + + public CUserNotFoundException(String msg) { + super(msg); + } + + public CUserNotFoundException() { + super(); + } +} diff --git a/src/main/java/com/rest/api/controller/v1/UserController.java b/src/main/java/com/rest/api/controller/v1/UserController.java index 0253ec4..3893260 100644 --- a/src/main/java/com/rest/api/controller/v1/UserController.java +++ b/src/main/java/com/rest/api/controller/v1/UserController.java @@ -1,5 +1,6 @@ package com.rest.api.controller.v1; +import com.rest.api.advice.exception.CUserNotFoundException; import com.rest.api.entity.User; import com.rest.api.model.response.BasicResult; import com.rest.api.model.response.CommonResult; @@ -32,7 +33,8 @@ public class UserController { @GetMapping(value = "/user/{userId}") public BasicResult findUserById(@ApiParam(value = "회원ID", required = true) @RequestParam int userId) { // 결과데이터가 단일건인경우 getBasicResult를 이용해서 결과를 출력한다. - return responseService.getBasicResult(userJpaRepo.findById(userId).orElse(null)); +// return responseService.getBasicResult(userJpaRepo.findById(userId).orElse(null)); + return responseService.getBasicResult(userJpaRepo.findById(userId).orElseThrow(CUserNotFoundException::new)); } @ApiOperation(value = "회원 입력", notes = "회원을 입력한다")