스프링 컨트롤러에서 익셉션 처리에 대해 궁금합니다.

5,332 views
Skip to first unread message

쿠이

unread,
Dec 11, 2013, 5:29:24 AM12/11/13
to ks...@googlegroups.com
안녕하세요.

현재 스프링으로 프로젝트를 진행하려고 하는데 컨트롤러에서 익셉션을 다들 어떻게 처리 하시는지 궁금합니다.

저는 우선 Dao 에서 생긴 익셉션과 Service 단에서 생긴 입셉션을 

1차적으로 throw 해서 컨트롤러로 넘긴다음에 컨트롤러에 try catch 문을 안쓰고 리퀘스트맵핑 메소드에 throws Exception 이렇게 해서

예외 리졸버로 에러라 났을 때 에러 페이지를 보여주는 식으로 했는데 이게 맞는 것인지 궁금합니다.

그리고 이렇게 했을 때 문제가 없는지 궁금합니다.

Shin-Hyeong Song

unread,
Dec 11, 2013, 7:47:46 PM12/11/13
to ks...@googlegroups.com
저같은 경우는 특별한 예외처리가 필요한 경우가 아니면.. 
@ControllerAdvice, @ExceptionHandler를 사용해서 한 클래스에서 모든 예외를 처리합니다..

컨트롤러 - 서비스 단에서 따로 Exception 던지거나 try catch 할 필요도 없지요..


2013년 12월 11일 오후 7:29, 쿠이 <phka...@gmail.com>님의 말:

--
Google 그룹스 'Korea Spring User Group' 그룹에 가입했으므로 본 메일이 전송되었습니다.
이 그룹에서 탈퇴하고 더 이상 이메일을 받지 않으려면 ksug+uns...@googlegroups.com에 이메일을 보내세요.
http://groups.google.com/group/ksug에서 그룹을 방문하세요.
더 많은 옵션을 보려면 https://groups.google.com/groups/opt_out을(를) 방문하세요.

namkyu Lee

unread,
Dec 11, 2013, 8:43:04 PM12/11/13
to ks...@googlegroups.com
저는 다음과 같이 exception resolver를 등록하여 Controller 에서 try catch로 예외를 잡지 않고, DispatcherServlet으로 모두 던집니다.

<!-- ============================================================= -->
<!-- exception resolver -->
<!-- ============================================================= -->
<bean class="com.kyu.svc.common.handler.CustomSimpleMappingExceptionResolver">
<property name="order" value="1" />
<property name="defaultErrorView" value="common/error/defaultError" />
<property name="exceptionMappings">
<props>
<prop key="BusinessLogicException">common/error/businessLogicError</prop>
<prop key="RuntimeException">common/error/runtimeError</prop>
<prop key="TypeMismatchException">common/error/defaultError</prop>
</props>
</property>
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
<property name="order" value="2" />
</bean>


2013년 12월 12일 오전 9:47, Shin-Hyeong Song <ken...@gmail.com>님의 말:

skplanet

unread,
Dec 11, 2013, 9:00:13 PM12/11/13
to ks...@googlegroups.com
이 방법 추천합니다~

쿠이

unread,
Dec 11, 2013, 11:28:38 PM12/11/13
to ks...@googlegroups.com
여기서 한가지 궁금한게 이와 같은 방법으로 할 경우

DAO 와 Service단에서 생긴 익셉션을 throw 익셉션 할 경우 컨트롤러로 올라가지 않고 익셉션리졸버에서 익셉션을 잡아주는건가요???

이용구

unread,
Dec 12, 2013, 2:24:37 AM12/12/13
to ks...@googlegroups.com
리졸버는 컨트롤러에서 빠져나온 뒤에 적용됩니다.

예를들어 RuntimeException이 발생하면 컨트롤러에서 exception page view로 넘기는게 MVC 로직인데 그걸 다른 로직을 타도록 직접 Resolver를 셋팅해 주는 방식입니다..


2013/12/12 쿠이 <phka...@gmail.com>
여기서 한가지 궁금한게 이와 같은 방법으로 할 경우

DAO 와 Service단에서 생긴 익셉션을 throw 익셉션 할 경우 컨트롤러로 올라가지 않고 익셉션리졸버에서 익셉션을 잡아주는건가요???

--
Google 그룹스 'Korea Spring User Group' 그룹에 가입했으므로 본 메일이 전송되었습니다.
이 그룹에서 탈퇴하고 더 이상 이메일을 받지 않으려면 ksug+uns...@googlegroups.com에 이메일을 보내세요.
http://groups.google.com/group/ksug에서 그룹을 방문하세요.
더 많은 옵션을 보려면 https://groups.google.com/groups/opt_out을(를) 방문하세요.



--
YK Lee (i&combine)

애너벨리

unread,
Dec 22, 2013, 7:24:42 PM12/22/13
to ks...@googlegroups.com
참고하시기 좋은 링크 하나 남겨드립니다

Reply all
Reply to author
Forward
0 new messages