首页 文章

使用HandlerExceptionResolver处理Spring MVC异常

提问于
浏览
5

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

我想通过 resolveException 处理正常异常以及通过 handleNoSuchRequestHandlingMethod 处理404 .

根据请求类型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 with 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 回答

  • 2

    根据Spring的Juergen Hoeller的说法, HandlerExceptionResolver 是不可能的,因为它仅适用于子映射,例如

    你有一个映射到 /account/** 的控制器,并从没有映射的acount访问一个方法,如 /acount/notExists ,它应该工作 .

    我将为此功能打开一张JIRA improvement

    EDIT:

    关于这个问题的JIRA门票

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

  • 3

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

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

    要重现相同的功能,您可以子类 DefaultHandlerExceptionResolver 并覆盖您需要的方法,或者需要在 resolveException 方法中添加一个处理 NoSuchRequestHandlingMethodException 的大小写 .

相关问题