Spring MVC 使用 HandlerExceptionResolver 进行异常处理

发布于 2024-12-02 01:11:16 字数 2872 浏览 2 评论 0 原文

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

我想通过 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;
  }

}

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

上面的代码仍然没有效果。

还有其他想法吗?

I am currently trying to use HandlerExceptionResolver for exception handling in a Spring MVC project.

I want to handle normal exceptions via resolveException as well as 404's via
handleNoSuchRequestHandlingMethod.

Depending on the request type JSON or text/html the exception response should be returned appropriately.

resolveException works now.

But handleNoSuchRequestHandlingMethod is giving me a headache. It's never called!

According to the docu the method should be called on 404 errors

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

What am I doing wrong...

This is what I have so far.

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

The above code still has no effect.

Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

四叶草在未来唯美盛开 2024-12-09 01:11:16

根据 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

According to Juergen Hoeller from Spring, it isn't possible with the HandlerExceptionResolver because it only works for sub-mapping e.g.

you have a controller mapped to /account/** and accesss a method from acount where no mapping exists like /acount/notExists than it should work.

I will open a JIRA improvement ticket for this functionality

EDIT:

JIRA ticket about this issue

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

冷情 2024-12-09 01:11:16

handleNoSuchRequestHandlingMethod 不是 HandlerExceptionResolver 接口的一部分,因此仅声明该名称的方法不会执行任何操作。它是特定于 DefaultHandlerExceptionResolver 的受保护方法,并从其 resolveException 方法(该是接口的一部分)调用:

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

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

handleNoSuchRequestHandlingMethod isn't part of the HandlerExceptionResolver interface, so just declaring a method of that name will do nothing. It's a protected method specific to DefaultHandlerExceptionResolver, and is called from its resolveException method (which is part of the interface):

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

To reproduce the same functionality, you can either subclass DefaultHandlerExceptionResolver and override the methods you need to, or you need to add a case in your resolveException method that handles NoSuchRequestHandlingMethodException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文