일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- arguements
- unmashalling
- eclipse pliugin
- 유용한 코드
- 유용한사용법
- 카멜룰
- svn ignore
- New Project
- JExcel
- camel rule
- window7
- BiffException
- 구동하기
- 로우병합
- mput
- put
- 초기데이터
- filed
- plan_table
- 유용한 표현
- javascripts
- TypeReference
- 인수배열
- WM_CONCAT
- json
- Marshalling
- spring boot
- XMLAGG
- Jackson
- 호출
- Today
- Total
하루에 한가지
egov (전자정부) ExceptionHandling 본문
전자정부 프레임을 쓰는데. package 구조를 변경하고, 처리해야할 contextType이 늘어 나면서 정상적으로 에러처리가 되지 않고 있다.
그리하여 해당 처리 부분에 대해서 알아보자.
일단 web.xml 에 에러 처리 페이지가 등록되어 있었다.
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/common/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/common/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
어쨋던 난 이 놈은 안 쓸꺼다.
dispatcher-servlet.xml 에 ExceptionHandler 가 붙어 있었다.
(아니다. Resolver가 붙어 있었다..)
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="cmmn/egovError"/>
<property name="exceptionMappings">
<props>
<prop key="org.springframework.dao.DataAccessException">cmmn/dataAccessFailure</prop>
<prop key="org.springframework.transaction.TransactionException">cmmn/transactionFailure</prop>
<prop key="egovframework.rte.fdl.cmmn.exception.EgovBizException">cmmn/egovError</prop>
<prop key="org.springframework.security.AccessDeniedException">cmmn/egovError</prop>
</props>
</property>
</bean>
에러가 발생을 하였을 경우 각각의 경우에 에러 페이지로 view를 resolving 하나보다.
그런데.. 난 에러 타입도 타입이지만. context-type에 따라서 분기되어 전달되는 것이 훨씬 중요하다.
(json type을 쓰고 있으므로...)
Controller advice를 써야 할까 ExceptionResolver를 써야 할까.. 고민이 되기는 하는데.
이미 위의 방식으로 적용이 되어 있으니 같은 방식으로 처리를 해보자.
SimpleMappingExceptionResolver 는 AbstractHandlerExceptionResolver을 상속하고 있다.
/**
* Actually resolve the given exception that got thrown during handler execution,
* returning a {@link ModelAndView} that represents a specific error page if appropriate.
* <p>May be overridden in subclasses, in order to apply specific exception checks.
* Note that this template method will be invoked <i>after</i> checking whether this
* resolved applies ("mappedHandlers" etc), so an implementation may simply proceed
* with its actual exception handling.
* @param request current HTTP request
* @param response current HTTP response
* @param handler the executed handler, or {@code null} if none chosen at the time
* of the exception (for example, if multipart resolution failed)
* @param ex the exception that got thrown during handler execution
* @return a corresponding {@code ModelAndView} to forward to, or {@code null} for default processing
*/
protected abstract ModelAndView doResolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex);
해러가 발생을 하면 위를 통해서 에러가 분기되는가 보다.
내 경우를 카버할 수 없으니, ExceptionResolver 하나 만드는 것이 편할 것 같다.
별다른 프로퍼티 전달받지 말고 그냥 클래스 안에 구현하도록 하자.
(뭐 필요하다 싶으면 그 때 빼고..)
해당 메서드의 특징은 ModelAndView를 리턴하고.( 내가 new 해서 주면 되는 듯.)
request, response, exception 등에 접근할 수 있다.
간단하게 구현하자, 나중에 수정을 하던가 하지 뭐..
public class CommHandlerExceptionResolver extends AbstractHandlerExceptionResolver{
private static final Logger LOGGER = LoggerFactory.getLogger(CommHandlerExceptionResolver.class);
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) {
ModelAndView mvn = new ModelAndView();
LOGGER.debug("## An exception has occurred!! ");
LOGGER.debug("## request.contentType : {}",request.getContentType());
LOGGER.debug("## request.requestURL : {}",request.getRequestURL());
if( request.getContentType().indexOf("application/json") >-1 ) {
//TODO Exception 정보를 바인딩 하시오.
mvn.setViewName("error/json/500");
}else if( request.getContentType().indexOf("text/html") >-1 ){
//TODO Exception 정보를 바인딩 하시오.
mvn.setViewName("error/jsp/500");
}else {
//TODO Exception 정보를 바인딩 하시오.
mvn.setViewName("error/jsp/500");
}
return mvn;
}
}
각각에 에러 페이지는 jsp로 구현하고 리턴 타입은
<%@ page contentType="application/json; charset=utf-8" pageEncoding="utf-8"%><%
을 통해서 설정하여 리턴.
이러면 에러페이지 설정은 끝내도 될 듯 하다.
- 끝-
'java > spring' 카테고리의 다른 글
spring을 알아보자 (0) | 2018.07.20 |
---|