此文章发布于79个月前,部分信息可能已经过时,请自行斟酌确认。
网上太多关于通过 @ControllerAdvice 处理全局异常的文章,但这种方式只能捕获 Controller 层的异常,如果是模板渲染发生异常(如模板文件不存在)则不会被 @ControllerAdvice 捕获到,查了一下资料没有找到理想的方法,临时只能先通过自定义 error.html 显示一下了。
有没有高手有好方法。
@ControllerAdviceannotation cannot work, becauseyou can only use it for exception inside a controller, and here you want to process exception in a view. I'm unsure forAbstractHandlerExceptionResolver, but doc states Spring HandlerExceptionResolverimplementations deal with unexpected exceptions that occur during controller execution. But Filter method should work.
参考: