spring-mvc - 如何在Spring中处理泛型异常

标签 spring-mvc exception-handling error-handling

我正在一个有几个类(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/

相关文章:

python - 在 Python 中使用内置 str() 类型的潜在异常

c# - 在ASP.NET C#项目中实现异常处理

python - IndexError : list index out of range - throwing error in Python?

Java Spring MVC 模板引擎

Java/Spring MVC 起始页面项目?

java - Spring Boot映射静态html

debugging - 预期的行为?除在 Debug模式下外,MATLAB会忽略反序列化时的错误

spring - 从 Controller 运行 Spring 批处理作业

python - 在 Python 2 中搜索 FileNotFoundError 的等价物

php - 捕获从 Postgresql 到 PHP 的错误