使用HandlerExceptionResolver处理Spring MVC异常

时间:2011-08-29 09:58:02

标签: spring-mvc exception-handling http-status-code-404

我目前正在尝试在Spring MVC项目中使用HandlerExceptionResolver进行异常处理。

我想通过resolveException以及404的via来处理正常的异常 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;
  }

}

使用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;
  }  
}

以上代码仍无效。

还有其他想法吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

handleNoSuchRequestHandlingMethod不是HandlerExceptionResolver接口的一部分,因此只声明该名称的方法将不起作用。它是特定于DefaultHandlerExceptionResolver的受保护方法,并从其resolveException方法( 接口的一部分)调用:

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

要重现相同的功能,您可以子类DefaultHandlerExceptionResolver并覆盖您需要的方法,或者需要在处理resolveException的{​​{1}}方法中添加一个案例。< / p>