l a*_*a s 4 java rest tomcat web-applications spring-mvc
我们是一个基于Spring MVC的REST应用程序.我试图使用ExceptionHandler注释来处理所有错误和异常.
我有
@ExceptionHandler(Throwable.class)
public @ResponseBody String handleErrors() {
return "error";
}
Run Code Online (Sandbox Code Playgroud)
只要抛出异常并且它不适用于任何错误,它就可以工作.
我使用的是Spring 4.0.有没有解决方法?
与ExceptionHandler#value()
属性指示的相反
Class<? extends Throwable>[] value() default {};
Run Code Online (Sandbox Code Playgroud)
并且@ExceptionHandler
仅用于处理Exception
及其子类型.
Spring使用ExceptionHandlerExceptionResolver
以下方法来解析带注释的处理程序
doResolveHandlerMethodException(HttpServletRequest request,
HttpServletResponse response, HandlerMethod handlerMethod, Exception exception)
Run Code Online (Sandbox Code Playgroud)
你可以看到只接受一个Exception
.
你不能处理Throwable
或Error
类型与@ExceptionHandler
此配置.
我会告诉你提供自己的HandlerExceptionResolver
实现来处理Throwable
实例,但你需要自己提供自己的DispatcherServlet
(和大多数MVC堆栈),因为在任何你可以产生重大差异的地方DispatcherServlet
没有catch
Throwable
实例.
更新:
从4.3开始,Spring MVC Throwable
在一个NestedServletException
实例中包含一个抛出的值并将其暴露给ExceptionHandlerExceptionResolver
.
归档时间: |
|
查看次数: |
4221 次 |
最近记录: |