2011-08-29 118 views
5

我目前正在嘗試使用HandlerExceptionResolver來處理Spring MVC項目中的異常。使用HandlerExceptionResolver處理Spring MVC異常

我想通過resolveException以及404的通過 handleNoSuchRequestHandlingMethod處理正常例外。

根據請求類型JSON或text/html,應該適當地返回異常響應。

resolveException現在工作。

handleNoSuchRequestHandlingMethod讓我頭疼。它從來沒有叫過!

按照實況方法應該叫上404錯誤

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html

我在做什麼錯?

這是我到目前爲止所。

public class JsonExceptionResolver implements HandlerExceptionResolver { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    public ModelAndView resolveException(HttpServletRequest request, 
    if (exception instanceof NoSuchRequestHandlingMethodException) { 
       return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException)    exception, request, response, handler); 
    } 
    ...     
    } 

    public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, 
     HttpServletRequest request, 
     HttpServletResponse response, 
     Object handler){ 

    logger.info("Handle my exception!!!"); 

    ModelAndView mav = new ModelAndView(); 
    boolean isJSON = request.getHeader("Accept").equals("application/json"); 

    if(isJSON){ 
    ... 

    }else{ 
    .. 
    } 

    return mav; 
    } 

} 

EDIT與DefaultHandlerExceptionResolver

public class MyExceptionResolver extends DefaultHandlerExceptionResolver { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    @Override 
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { 
    logger.warn("An Exception has occured in the application", exception); 


    logger.info("exception thrown " + exception.getMessage()); 
    if (exception instanceof NoSuchRequestHandlingMethodException) { 
     return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler); 
    } 

    ... 
    return mav; 
    } 

    public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, 
     HttpServletRequest request, 
     HttpServletResponse response, 
     Object handler){ 

    logger.info("Handle my exception!!!"); 

    ModelAndView mav = new ModelAndView(); 
    boolean isJSON = request.getHeader("Accept").equals("application/json"); 

    if(isJSON){ 

     ... 
    }else{ 
     ... 
    } 

    return mav; 
    } 
} 

上面的代碼仍然沒有效果。

還有其他想法嗎?

回答

3

根據Spring的Juergen Hoeller的說法,HandlerExceptionResolver是不可能的,因爲它只適用於子映射,

您有一個映射到/account/**的控制器,並從acount訪問一個方法,其中不存在像/acount/notExists那樣的映射。

我會打開一個JIRA 改善票這個功能

編輯:

JIRA票這個問題

https://jira.springsource.org/browse/SPR-8837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=72648#comment-72648

2

handleNoSuchRequestHandlingMethod不是HandlerExceptionResolver接口的一部分,因此只聲明該名稱的方法將不會執行任何操作。這是特定於DefaultHandlerExceptionResolver受保護的方法,並從其resolveException方法調用(其中是接口的部分):

if (ex instanceof NoSuchRequestHandlingMethodException) { 
    return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response, handler); 
} 

要重現相同的功能,您可以子類DefaultHandlerExceptionResolver並覆蓋你需要的方法到,或者您需要在處理NoSuchRequestHandlingMethodExceptionresolveException方法中添加一個案例。

+0

我更新了我的代碼。你的意思是這樣嗎? –

+1

我也嘗試過從''DefaultHandlerExceptionResolver''繼承,但是在404的情況下''doResolveException''永遠不會被調用......我誤解了你嗎? –