API์์์ ์์ธ ์ฒ๋ฆฌ
by rlaehddnd0422์ ํ๋ฆฌ์ผ์ด์ ์์ ์ปจํธ๋กค๋ฌ์์ ์์ธ๊ฐ ๋ฐ์ํ์ ๋ HTML ํ์ด์ง์ ๊ฒฝ์ฐ์๋ 4xx, 400, 5xx .. ์ ๊ฐ์ ์ค๋ฅํ์ด์ง ์ค์ ์ ํตํด ์คํ๋ง ๋ถํธ์ ํ์ดํธ๋ผ๋ฒจ ํ์ด์ง๋ ํฐ์บฃ์ ์ค๋ฅํ๋ฉด์ ์ปค์คํ ํด์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด์ฌ์ค ์ ์์์ต๋๋ค.
ํ์ง๋ง API์ ๊ฒฝ์ฐ์๋ ๊ฐ ์ค๋ฅ ์ํฉ์ ๋ง๋ ์ค๋ฅ ์๋ต ์คํ์ ์ ํ๊ณ , JSON์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ด๋ ค์ฃผ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ์์ธ์ฒ๋ฆฌ์ ์๊ฐํ ๋ถ๋ถ์ด ๋ง์ต๋๋ค.
์ฐ์ API๋ ์ค๋ฅ ํ์ด์ง์์ ํ๋ ๊ฒ์ฒ๋ผ ์๋ธ๋ฆฟ ์ค๋ฅ ํ์ด์ง ๋ฐฉ์์ ๋จผ์ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
@Slf4j
@RestController
public class ApiExceptionController {
@Data
@AllArgsConstructor
static class MemberDto
{
private String memberId;
private String name;
}
@GetMapping("/api/members/{id}")
public MemberDto getMember(@PathVariable("id") String id)
{
if(id.equals("ex"))
{
throw new RuntimeException("์๋ชป๋ ์ฌ์ฉ์");
}
return new MemberDto(id,"hello "+id);
}
API๋ฅผ ์์ฒญํ์ ๋
- ์ ์์์ฒญ : JSON ์ ๋ฐํ๋จ
- ์ค๋ฅ์์ฒญ : ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋ ์ค๋ฅ ํ์ด์ง HTML์ด ๋ฐํ
ํด๋ผ์ด์ธํธ๊ฐ ์ ์ ์์ฒญ์ด๋, ์ค๋ฅ ์์ฒญ์ด๋ JSON์ด ๋ฐํ๋๊ธธ ๊ธฐ๋ํ ๋ ์ด๋ป๊ฒ ํด์ผํ ๊น์?
API ์๋ต์ ์ถ๊ฐํด์ ์ค๋ฅ ์์ฒญ์ผ ๋ ์ค๋ฅ ์ ๋ณด JSON์ผ๋ก ๋ฐํ
@RequestMapping(value = "/error-page/500", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String,Object>> errorPage500Api(HttpServletRequest request, HttpServletResponse response) {
log.info("errorPage 500 API");
Map<String, Object> result = new HashMap<>();
Exception ex = (Exception) request.getAttribute(ERROR_EXCEPTION);
result.put("status",request.getAttribute(ERROR_STATUS_CODE));
result.put("message",ex.getMessage());
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
return new ResponseEntity(result, HttpStatus.valueOf(statusCode));
}
- ๋งคํ ์ ๋ณด์ produces ์ต์
์ JSON์ผ๋ก ์ง์ ํ๋ฉด Request์ Acceptํค๋๊ฐ application/json์ผ ๊ฒฝ์ฐ์ ํด๋น ๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค.
- Jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ResponseEntity๋ฅผ ์ฌ์ฉํด์ ์๋ตํ๊ธฐ ๋๋ฌธ์ ๋ฉ์์ง ์ปจ๋ฒํฐ๊ฐ ๋์ํ๋ฉด์ ํด๋ผ์ด์ธํธ JSON์ด ๋ฐํ.
- ResponseEntity์ ์ค๋ฅ ์ํ์ฝ๋, ๋ฉ์์ง๋ฅผ json ์ผ๋ก ๋ฐํํ์ต๋๋ค.
์คํ๋ง ๋ถํธ๊ฐ ์ ๊ณตํ๋ ๊ธฐ๋ณธ ์ค๋ฅ ๋ฐฉ์ ์ฌ์ฉ - BasicErrorController
Customizing ํ ์ค๋ฅ ํ์ด์ง๋ฅผ ์ฌ์ฉํ์ง ์์ ๋๋, ์คํ๋ง ๋ถํธ๋ BasicErrorController๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
BasicErrorController๋ฅผ ์ฌ์ฉ ์
Acceptํค๋๊ฐ application/json ์ด๋ฉด json์ ์ค๋ฅ์ ๋ณด๋ฅผ ๋ด์ ๋ฆฌํดํ๊ณ ,
text/html์ด๋ฉด ๋ํดํธ๋ก ๋งคํ๋ /error ๊ฒฝ๋ก์ ์๋ ๋ทฐ๋ฅผ ์ฐพ์ ์๋ฌ์ฝ๋์ ๋งค์นญ๋๋ ๋ทฐ๋ฅผ ์ฐพ์ ๋ ๋๋งํฉ๋๋ค.
+ application.properties์ ์ถ๊ฐ์ ์ผ๋ก ์ค๋ฅ ์ ๋ณด๋ฅผ ์ถ๊ฐํ ์ ์์ง๋ง
server.error.include-binding-errors=always
server.error.include-exception=true
server.error.include-message=always
server.error.include-stacktrace=always
๋ณด์ ์ ์ํํ๊ธฐ ๋๋ฌธ์ ์ค๋ฅ์ ๋ํด์๋ ๊ฐ๊ฒฐํ ๋ฉ์์ง๋ง ๋ ธ์ถํ๊ณ ๋ก๊ทธ๋ฅผ ํตํด์ ํ์ธํ๋๋ก ํฉ์๋ค.
๋ค์์ ๋ค๋ฃฐ @ExceptionHandler๊ฐ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ๋์ ๋ฐฉ๋ฒ์ด๊ธฐ ๋๋ฌธ์ ์ง๊ธ์ ์ด ์ ๋๊น์ง๋ง ์์๋ณด๊ฒ ์ต๋๋ค.
HandlerExceptionResolver
๋์ ์์
- ์ปจํธ๋กค๋ฌ์ ์์ธ๊ฐ ๋ฐ์ํด์ WAS๊น์ง ์์ธ๊ฐ ์ ํ๋์์ ๋ HTTP ์ํ์ฝ๋๋ 500 (์๋ฒ ์ค๋ฅ)๋ก ์ฒ๋ฆฌ๋๋๋ฐ ์์ธ์ ๋ฐ๋ผ 400, 404 ๋ฑ๋ฑ ๋ค๋ฅธ ์ํ์ฝ๋๋ก ์ฒ๋ฆฌํ๊ณ ์ถ์ ๋
- ์ค๋ฅ ๋ฉ์์ง๋ ํ์์ API๋ง๋ค ๋ค๋ฅด๊ฒ ์ฒ๋ฆฌํ๊ณ ์ถ์ ๋
HandlerExceptionResolver๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค.
1. HandlerExceptionResolver๋ฅผ ์์๋ฐ์ ExceptionReseolver ์์ฑ
@Slf4j
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {
try
{
if(ex instanceof IllegalStateException)
{
log.info("IllegalArgumentException resolver to 400");
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
return new ModelAndView();
}
}catch(IOException e)
{
log.error("resolver ex" , e);
}
return null;
}
}
2. WebConfig์ ๋ฑ๋ก
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
resolvers.add(new MyHandlerExceptionResolver());
}
์ด๋ ๊ฒ ExceptionResolver๋ฅผ ์ฌ์ฉํด์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋ ์์ธ์ ๋ณด๋ฅผ ๋ฐ๊พธ์ด ํด๋ผ์ด์ธํธ์๊ฒ ๋๊ธธ ์ ์์ต๋๋ค.
- ๋น ModelAndView() ๋ฆฌํด ์ ๋ทฐ๋ฅผ ๋ ๋๋ง ํ์ง์๊ณ ์ ์ ํ๋ฆ์ผ๋ก ์๋ธ๋ฆฟ์ผ๋ก ๋ฆฌํด.
- null ๋ฆฌํด : ๋ค์ ExceptionResolver๊ฐ ์์ ๊ฒฝ์ฐ ์ฒ๋ฆฌํ์ง ๋ชปํ ์์ธ๋ฅผ ๋๊ฒจ์ ์คํ. ๋ค์ ExceptionResolver๊ฐ ์์ผ๋ฉด ์์ธ๋ฅผ ์๋ธ๋ฆฟ ๋ฐ์ผ๋ก ๋์ง๋๋ค.
ExceptionResolver๋ฅผ ์ ํ์ฉํ๋ฉด ์ด๋ฐ ์ฌ์๋ค์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
- ์์ธ ์ํ ์ฝ๋ ๋ณํ (response.sendError(...) )
- ๋ทฐ ํ ํ๋ฆฟ ์ฒ๋ฆฌ ( ๋ชจ๋ธ๋ทฐ์ ๊ฐ์ ์ฑ์์ ) : WAS
- API ์๋ต์ฒ๋ฆฌ (response.getWriter().println("hello"); ์ฒ๋ผ HTTP ์๋ต ๋ฐ๋์ ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด์ฃผ๋ ๊ฒ๋ ๊ฐ๋ฅ. ์ฌ๊ธฐ์ JSON์ผ๋ก ์๋ตํ๋ฉด API ์๋ต์ฒ๋ฆฌ๋ฅผ ํ ์ ์์ต๋๋ค.)
์ถ๊ฐ๋ก ์ปจํธ๋กค๋ฌ์์ ์์ธ๊ฐ ๋ฐ์ํด๋ ExceptionResolver๋ฅผ ์ฌ์ฉํ๋ฉด ์์ธ๊ฐ WAS๊น์ง ์ ํ๋์ง ์๊ณ ์๋ธ๋ฆฟ ์ ์์ ํด๊ฒฐ๋๊ธฐ ๋๋ฌธ์ ์๋นํ ๊น๋ํด์ง๋ค๋ ๋ฉ๋ฆฌํธ๊ฐ ์์ต๋๋ค.
ํ์ง๋ง ์ด๋ ๊ฒ ์ธํฐํ์ด์ค๋ฅผ ๋ด๋ ค๋ฐ์ ๊ตฌํํ๊ณ ๋ก์ง์ง๊ณ ๋ฑ๋กํ๋ ๊ณผ์ ์์ฒด๊ฐ ์ข ๊ท์ฐฎ์ ์ผ์ ๋๋ค.
์คํ๋ง ๋ถํธ๊ฐ ์ ๊ณตํ๋ ExceptionResolver
1. DefaultHandlerExceptionResolver
- ์คํ๋ง ๋ด๋ถ์์ ๋ฐ์ํ๋ ์คํ๋ง ์์ธ๋ฅผ ํด๊ฒฐ
- ๋ํ์ ์ผ๋ก ํ๋ผ๋ฏธํฐ ๋ฐ์ธ๋ฉ ์์ ์ ํ์ ์ด ๋ง์ง ์์ผ๋ฉด ๋ด๋ถ์์ TypeMistmatchException์ด ๋ฐ์ํ๋๋ฐ, ์ด ๊ฒฝ์ฐ ์๋ธ๋ฆฟ ์ปจํ ์ด๋๊น์ง ์ค๋ฅ๊ฐ ์ฌ๋ผ๊ฐ๊ณ , ๊ฒฐ๊ณผ์ ์ผ๋ก 500์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
- ํ์ง๋ง ์ฌ์ค ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ ฅ๊ฐ์ ํ์ ์ ๋ง์ง ์๊ฒ ์ ๋ ฅํ๊ธฐ ๋๋ฌธ์ 400 ์๋ฌ๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฆฌํด ํด์ฃผ์ด์ผํ๋๋ฐ ์ด๋ฐ ์ญํ ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ํ ํด์ฃผ๋ Resolver๊ฐ DefaultHandlerExceptionResolver
@GetMapping("/api/default-handler-ex")
public String defaultException(@RequestParam Integer data)
{
return "ok";
}
2. ResponseStatusExceptionHandler
- ResponseStatusExceptionHandler๋ ์์ธ์ ๋ฐ๋ผ์ HTTP ์ํ์ฝ๋๋ฅผ ์ง์ ํด์ฃผ๋ ์ญํ ์ ํฉ๋๋ค.
- @ResponseStatus๊ฐ ๋ฌ๋ ค์๋ ์์ธ์ ResponseStatusException ์์ธ ๋ ๊ฐ์ง ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
์์ธ์ @ResponseStatus ์ด๋ ธํ ์ด์ ์ ์ ์ฉํ๋ฉด HTTP ์ํ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
@ResponseStatus(code= HttpStatus.BAD_REQUEST, reason = "์๋ชป๋ ์์ฒญ ์ค๋ฅ")
public class BadRequestException extends RuntimeException{
}
- BadRequestException์ RuntimeException(500 ์๋ฒ ์๋ฌ)๋ฅผ ์์๋ฐ์ ์๋ฌ.
- ์๋ ๊ฐ์ผ๋ฉด ๋ง์ฐฌ๊ฐ์ง๋ก ์์๋ฐ์๊ธฐ ๋๋ฌธ์ ์ํ์ฝ๋๊ฐ 500๋ ์ฌ์ผ ํ์ง๋ง @ResponseStatus์์ ์ํ์ฝ๋๋ฅผ ์ํ๋ ๋๋ก ์ง์ ํ๋ฉด ResponseStatusExceptionResolver๊ฐ ํด๋น ์ด๋ ธํ ์ด์ ์ ํ์ธํ๊ณ ์ค๋ฅ์ฝ๋๋ฅผ ์ง์ ํ ์ฝ๋๋ก ๋ณ๊ฒฝํ๊ณ reason์ ํตํด ๋ฉ์์ง์ ๋ด์ต๋๋ค.
์์ธ์ @ResponseStatus๋ฅผ ๋ฌ์ HTTP ์ํ์ฝ๋ ์ง์ ํ๊ธฐ
@GetMapping("/api/response-status-ex1")
public String responseStatusEx1()
{
throw new BadRequestException();
}
reason์ MessageSource์์ ์ฐพ๋ ๊ธฐ๋ฅ๋ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ messages.properties๋ก ์ฒ๋ฆฌํ ์๋ ์์ต๋๋ค.
ResponseStatusException ์์ธ ๋์ง๊ธฐ
@GetMapping("/api/response-status-ex2")
public String responseStatusEx2()
{
throw new ResponseStatusException(HttpStatus.NOT_FOUND,"์๋ชป๋ ์์ฒญ ์ค๋ฅ", new IllegalArgumentException());
}
ResponseStatusException์ ์ง์ ์ค์ ํ๊ณ ์ ํ๋ ์ํ์ฝ๋์ ์ง์ ํ ์ค๋ฅ๋ฅผ ๋งค์นญ์์ผ ๋์ง๋ ๋ฐฉ๋ฒ
3. ExceptionHandlerExceptionResolver - @ExceptionHandler
์น ๋ธ๋ผ์ฐ์ ์ HTML ํ๋ฉด์ค๋ฅ๋ฅผ ์ ๊ณตํ ๋์๋ ์คํ๋ง ๋ถํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก BasicErrorController๋ฅผ ํธ์ถํด /error ๊ฒฝ๋ก์ ๋ง๋ค์ด๋ ๋ทฐํ ํ๋ฆฟ์ ๋ ๋๋ง ํ๋ค๊ณ ํ์์ต๋๋ค.
ํ์ง๋ง API ์ค๋ฅ์ ๊ฒฝ์ฐ์๋ ์๊ธฐ๊ฐ ์ข ๋ค๋ฆ ๋๋ค. ๊ฐ API๋ ์์คํ ๋ง๋ค ์๋ต์ ๋ชจ์๋, ์คํ๋ ๋ชจ๋ ๋ค๋ฅด์ฃ . ๋ฐ๋ผ์ ์์ธ ์ํฉ์ ๋จ์ํ ์น๋ธ๋ผ์ฐ์ ์ ์ค๋ฅํ๋ฉด HTML ์ ๋ณด์ฌ์ฃผ๋ ๊ฒ ์๋๋ผ ์์ธ์ ๋ฐ๋ผ์ ๊ฐ๊ฐ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํด์ผ ํ ์๋ ์์ต๋๋ค.
๋ ๊ฐ์ ์์ธ๋ผ๊ณ ํด๋ ์์ธ๊ฐ ๋ฐ์ํ ์ปจํธ๋กค๋ฌ๋ณ๋ก ๋ค๋ฅธ ์๋ต์ ๋ด๋ ค์ฃผ์ด์ผ ํ ์๋ ์์ด์.( ์๋ฅผ ๋ค๋ฉด ์ํ ๊ด๋ฆฌ ์ปจํธ๋กค๋ฌ์ ํ์ ๊ด๋ฆฌ ์ปจํธ๋กค๋ฌ์์์ RuntimeException ์์ธ์ ๋ํด ์๋ก ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก์ฒ๋ฆฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ).
์์ธ์ฒ๋ฆฌ์ ๋ํ ์ธ๋ฐํ ์ ์ด๊ฐ ํ์ํ ์์ ์ ์ฌ์ฉํ ์ ์๋ ํด๊ฒฐ์ฑ ์ด ๋ฐ๋ก ExceptionHandlerExceptionResolver๊ฐ ์ ๊ณตํ๋ @ExceptionHandler ์ ๋๋ค!
@ExceptionHandler ์ฅ์
- ๋ค๋ฅธ ์์ธ์ฒ๋ฆฌ ๋๊ตฌ๋ณด๋ค ์ฐ์ ์์๊ฐ ๋์ต๋๋ค.
- ๊ตฌํ์ด ๊ฐ๋จํฉ๋๋ค!
- ์คํ๋ง์ ์ปจํธ๋กค๋ฌ์ฒ๋ผ ๋ค์ํ ํ๋ผ๋ฏธํฐ์ ์๋ต ํ์ ์ ์ง์ ํ ์ ์์ต๋๋ค.
@ExceptionHandler์์ ํ๋ผ๋ฏธํฐ์ ์๋ตํ์
Web on Servlet Stack
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, "Spring Web MVC," comes from the name of its source module (spring-webmvc), but it is more commonl
docs.spring.io
@ExceptionHandler // UserException.class ์๋ต ๊ฐ๋ฅ
public ResponseEntity<ErrorResult> userExHandler(UserException e)
{
log.error("[exceptionHandler] ex", e);
ErrorResult result = new ErrorResult("USER-EX",e.getMessage());
return new ResponseEntity(result,HttpStatus.BAD_REQUEST);
}
@GetMapping("/api2/members/{id}")
public ApiExceptionController.MemberDto getMember(@PathVariable("id") String id)
{
if(id.equals("user-ex"))
{
throw new UserException("์ฌ์ฉ์ ์ค๋ฅ");
}
์ฌ์ฉ๋ฒ์ ๊ฐ๋จํฉ๋๋ค.
- @ExceptionHandler ์ด๋ ธํ ์ด์ ์ ์ ์ธํ๊ณ ํด๋น ์ปจํธ๋กค๋ฌ์์ ์ฒ๋ฆฌํ๊ณ ์ถ์ ์์ธ๋ฅผ ์ง์ .
- ์ด๋ ํ๋ผ๋ฏธํฐ์ ์
๋ ฅํ ์์ธ์ @ExceptionHandler์ ์ง์ ํ ์์ธ๊ฐ ๊ฐ์ผ๋ฉด @ExceptionHandler์ ์์ธ ํด๋์ค ์ด๋ฆ ์ง์ ์๋ต ๊ฐ๋ฅ.
@ExceptionHandler({AException.class, BException.class}) ๋ค์ํ ์์ธ๋ฅผ ํ๋ฒ์ ์ฒ๋ฆฌ๋ ๊ฐ๋ฅํฉ๋๋ค.
- ์์ ์์ธ ํด๋์ค๋ฅผ @ExceptionHandler๋ก ์ง์ ํ๋ฉด ํ์ ์์ธ ํด๋์ค๋ฅผ ์์ฐ๋ฅผ ์ ์์ต๋๋ค.
@ExceptionHandler(์์์์ธ.class)
public ~ ๋ถ๋ชจ์์ธ์ฒ๋ฆฌ(์์ ์์ธ e)
{}
@ExceptionHandler(ํ์์์ธ.class)
public ~ ํ์์์ธ์ฒ๋ฆฌ(ํ์ ์์ธ e)
{}
- ์คํ๋ง์ ์ฐ์ ์์๋ ํญ์ ์์ธํ ๊ฒ์ด ์ฐ์ ๊ถ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ํ์ ์์ธ ์ฒ๋ฆฌ๊ฐ ํธ์ถ๋ฉ๋๋ค.
- ์ด ์ฐ์ ์์๋ฅผ ์ ํ์ฉํด ์ฒ๋ฆฌํ์ง ๋ชปํ ๋ชจ๋ ์์ธ๋ค์ ๋ชจ๋ ์์ธ์ ๋ถ๋ชจ ์์ธ์ธ Exception์ @ExceptionHandler๋ก ์ฒ๋ฆฌํด์ ๋จ์ ์์ธ๋ค์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
+ @ControllerAdvice
@Slf4j
@RestControllerAdvice(annotations = RestController.class)
public class ExControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class) // @ResponseBody ์๋ ์ ์ฉ
public ErrorResult illegalExHandler(IllegalArgumentException e)
{
log.error("[exceptionHandler] ex", e);
return new ErrorResult("BAD",e.getMessage()); // json
}
@ExceptionHandler // UserException.class ์๋ต ๊ฐ๋ฅ
public ResponseEntity<ErrorResult> userExHandler(UserException e)
{
log.error("[exceptionHandler] ex", e);
ErrorResult result = new ErrorResult("USER-EX",e.getMessage());
return new ResponseEntity(result,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๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค.
- ๋์์ผ๋ก ์ง์ ํ์ง ์์ผ๋ฉด ๋ชจ๋ ์ปจํธ๋กค๋ฌ์ ์ ์ฉ
- @RestControllerAdvice = @ResponseBody + @ControllerAdvice
<์ฐธ๊ณ ์๋ฃ>
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-2/dashboard
'๐ Backend > MVC Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํฌ๋งทํฐ(Formatter) (0) | 2023.03.29 |
---|---|
ํ์ ์ปจ๋ฒํฐ(Type Converter) (0) | 2023.03.29 |
Spring - ์๋ธ๋ฆฟ ์์ธ ์ฒ๋ฆฌ ( Exception ) (0) | 2023.03.28 |
ArgumentResolver ํ์ฉ (0) | 2023.03.24 |
์คํ๋ง ์ธํฐ์ ํฐ(Interceptor) (0) | 2023.03.24 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422