基本处理流程图
image.png
自定义异常页面
我们自定义一个错误页面吧,比如我想用Thymeleaf来渲染,那得导入这个包,然后我们知道错误错误视图名字叫做error,所以得有这样一个页面,创建一个error.html放在resources/templates/下:
image.png
内容就是:
image.png
这些属性哪里来的,debug里看到的,其实是过程当中一步步设置进去的啦,有兴趣你可以跟一下:
image.png
最终结果:
image.png
自定义异常处理方法
自定义异常增强类,处理方法得有ExceptionHandler注解,注解属性里面也可以定义异常类型,不过这个只能用于HandlerMethod处理:
image.png
处理方法:
image.png
my_error.html:
image.png
为什么只能用于HandlerMethod处理,就看ExceptionHandlerExceptionResolver的匹配方法:
image.png
然后在处理的时候有这么一段,就是获取我们自定义的异常处理方法,封装成ServletInvocableHandlerMethod对象:
image.png
最后调用:
image.png
得到模型视图结果返回:
image.png
最后ThymeleafView会解析出视图然后渲染:
image.png
结果就是:
image.png
不是
HandlerMethod处理的就是这样:
image.png









网友评论