Spring 3控制器异常处理程序实现问题 [英] Spring 3 controller exception handler implementation problems

查看:96
本文介绍了Spring 3控制器异常处理程序实现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望实现一个ExceptionController来处理在执行其他控制器方法时抛出的异常。我没有在我的应用程序上下文中指定任何HandlerExceptionResolver,所以根据 API文档应该启动AnnotationMethodHandlerExceptionResolver。我在源码中验证了这一点。那么为什么以下的工作呢?

I was hoping to implement a single "ExceptionController" to handle exceptions that are thrown in execution of my other controllers' methods. I hadn't specified any HandlerExceptionResolver in my application context so according to the API documentation the AnnotationMethodHandlerExceptionResolver should be started. I verified it as such in the source. So why doesn't the following work?

@Controller
public class ExceptionController {

  @ExceptionHandler(NullPointerException.class)
  public ModelAndView handleNullPointerException(NullPointerException ex) {
    // Do some stuff
    log.error(logging stuff)
    return myModelAndView;
  }
}

@Controller
public class AnotherController {

  @RequestMapping(value="/nullpointerpath")
  public String throwNullPointer() {
    throw new NullPointerException();
  }
}

我在调试日志中看到三个默认异常处理程序被要求处理异常,但没有任何操作,我看到DispatcherServlet - 无法完成请求。其次是用户正在显示堆栈跟踪和500内部错误。

I see in the debug logs that the three default exception handlers are asked for handling of the exception, but nothing is done and I see "DispatcherServlet - Could not complete request". Followed by the user being displayed the stacktrace and a 500 Internal error.

推荐答案

确保您的异常处理程序返回一个存在/映射到一个处理程序的视图。

Make sure your Exception handler is returning a view that exists/maps to a handler.

这篇关于Spring 3控制器异常处理程序实现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆