我正在一个有几个类(class)的 Spring 项目。现在,该项目中的代码不符合标准,因为在任何类中都没有定义try catch块。
我的web.xml中有以下映射
<error-page>
<exception-code>404</exception-type>
<location>/error.do</location>
</error-page>
/error.do映射到一个 Controller ,从该 Controller 转发到通用错误 View 。现在,将有可能获取我的异常的堆栈跟踪。我唯一的要求是在该错误处理程序中获取异常详细信息,例如-异常起源于类名称,方法名称和异常消息,我认为这些异常消息将在堆栈跟踪中可用。
任何想法如何进行此问题陈述?
最佳答案
是的,您可以做到这一点Spring提供了@ExceptionHandler注释,因此在您的 Controller 中编写一个方法为:
@ExceptionHandler(YourException.class)
public ModelAndView handleYourException(YourException ex, HttpServletRequest request) {
mav = new ModelAndView();
mav.addObject("exception", exception);
mav.setViewName("YourException Page");
return mav;
}
希望你明白我的意思。
关于spring-mvc - 如何在Spring中处理泛型异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430507/