본문 바로가기

프레임워크/Spring boot

throws시 어떤 일이 발생될것인가 ?

가정

  • 서비스 내에서 Throws를 발생시킨다.
  • 그 Throws를 잡는 Controller @ExceptionHandler가 존재한다.

이제 내가 한 코드를 예를 들어서 설명해보겠다.

과정

Proxy객체의 throw ex.getTargetException() 함수를 호출한다.

Dispatcher Servlet객체까지 올라가 processDispatchResult() 함수를 호출한다.

Handler Exception을 처리하기 위한 processHandlerException()을 호출한다.

resolver의 resolveException()을 호출한다.

handlerExceptionResolver가 resolveException()함수를 호출한다.

exceptionHandlerMethod.invokeAndHandle()함수를 실행시킨 후 위에서 @ExceptionHandler annotation이 붙은 Method를 실행시킨다.

해당 에러를 처리할 Method를 찾은 후 해당 객체를 처리한다.

그 이후 Dispatcher Servlet은

해당 응답이 Json이라면 JsonSerializer을 호출하고 해당 Exception을 처리한 응답을 반환한다.

결론

  • 위의 화면을 보면 이해가 쉽다.
  • 요청 -> dispatcher servlet -> handler mappings를 통해 hanlder을 찾은 후 Handler 실행 -> 해당 handler는 Proxy객체화 -> method.invoke() 함수 실행 -> 예외 발생 -> method.invoke()에서 throw처리 -> 해당 throw를 처리할 Resolver 호출(handlerExceptionResolver) -> exceptionHandler Method를 찾은 후 에러 처리 -> 적절한 JsonSerializer를 찾은후 해당 Serializer에 구현된 Json Format으로 변환 -> 응답값 반환
  • 위와 같은 순서로 이루어진다 생각하면 된다.

'프레임워크 > Spring boot' 카테고리의 다른 글

Spring Boot는 Run시에 무엇을 할까 - Spring AOP  (0) 2022.08.14
Spring 주절주절(지속적 업데이트)  (0) 2022.08.11
Spring Validation과 JsonSerializer<Errors>  (0) 2022.08.01
Spring hateoas  (0) 2022.08.01
예외 처리  (0) 2022.07.14