기본 컨트롤러를 하나 만들고, 컨트롤러의 Response에 담아 줄 객체를 만들어 보자
컨트롤러와 객체는 아래와 같이 만들어주었다
// 컨트롤러 클래스 package com.example.springcontrolleradvice.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/orders") public class OrderController { @GetMapping("/{orderId}") public ResponseEntity<CommonResponse> getOrder(@PathVariable String orderId) { return ResponseEntity.ok(new CommonResponse("success", "order found", orderId)); } }
/orders/{orderId}
로 GET 요청을 보내면, orderId
를 Response에 담아서 보내준다// 컨트롤러의 Response 객체 package com.example.springcontrolleradvice.controller; public record CommonResponse( String Status, String message, Object data ) {}
컨트롤러에서 orderId가 100이 아닌 경우에는 Exception을 발생시켜보자
package com.example.springcontrolleradvice.controller;
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;
@RestController @RequestMapping(“/orders”) public class OrderController {
@GetMapping("/{orderId}") public ResponseEntity<CommonResponse> getOrder(@PathVariable String orderId) throws Exception { if(!orderId.equals("100")){ throw new Exception("order not found"); } return ResponseEntity.ok(new CommonResponse("success", "order found", orderId)); }
}
- 위처럼 Exception을 적용시키면, orderID가 100이 아닌경우에는 Exception 이 로그에 나온다 ### Custom Exception 만들기 - 발생하는 Exception을 상속받아 CustomException을 만들어보자 ```java package com.example.springcontrolleradvice.controller.exception; public class CustomException extends RuntimeException { private String message; private String errorCode; private String hint; protected CustomException() { } public CustomException(String message, String errorCode, String hint) { this.message = message; this.errorCode = errorCode; this.hint = hint; } // getters and setters }
package com.example.springcontrolleradvice.controller; import com.example.springcontrolleradvice.controller.exception.CustomException; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController RequestMapping("/orders") public class OrderController { @GetMapping("/{orderId}") public ResponseEntity<CommonResponse> getOrder(@PathVariable String orderId) { if (!orderId.equals("100")) { throw new CustomException("order not found", "ERROR400", "please check the order id"); } return ResponseEntity.ok(new CommonResponse("success", "order found", orderId)); } }
위의 과정은 Exception 발생시 Exception만 발생한다. 우리가 원하는 CustomException 메시지는 return되지 않는다.
Exception을 발생시킬 때, 리턴되는 메시지에 우리의 CustomException을 넣어주도록 하자
Exception을 intercept 해 주는 곳을 만들어서 우리의 CustomException을 return하도록 하자
CustomExceptionInterceptor
를 작성하자
package com.example.springcontrolleradvice.controller.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice public class CustomExceptionInterceptor extends ResponseEntityExceptionHandler {
@ExceptionHandler(CustomException.class) public final ResponseEntity<Object> handleCustomException(CustomException ex) { CustomExceptionModel exceptionResponse = new CustomExceptionModel(ex.getMessage(), ex.getErrorCode(), ex.getHint()); return new ResponseEntity(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR); }
}
- `@ControllerAdvice`를 사용해서 Exception을 intercept 해주는 곳을 만들어준다 - `ResponseEntityExceptionHandler`를 상속받아 CustomeExceptionInterceptor가 Exception이 발생 할 때 가로 채 주도록 만들자 - `CustomExceptionModel`은 `CustomException`과 동일한 구조를 가지고있는 메시지객체로 작성 해준다 ```java package com.example.springcontrolleradvice.controller.exception; public record CustomExceptionModel( String message, String errorCode, String hint ) { } ``` - 우리가 작성한 메시지로 exception을 return 해준다 export const _frontmatter = {"title":"SpringBoot Exception 처리하기","category":"Backend Engineering","author":"울이","tags":["#Spring","#Java","#exception","#custom-exception"],"date":"2022-12-12T00:00:00.000Z","keywords":["Spring","Java","exception","custom-exception"],"thumbnail":"springboot-exception-custom.jpg","slug":"backend/spring-boot/springboot-custom-exception","featured":true}