spring版本:5.0.6
多个处理器的两种情况
1. 存在一个类中
@restcontrolleradvice public class exceptionhandle { @exceptionhandler(exception.class) public object handle(exception e){ e.printstacktrace(); hashmap<string, object> map = new hashmap<>(); map.put("location", "exception"); map.put("msg", e.getmessage()); map.put("localmsg", e.getlocalizedmessage()); return map; } @exceptionhandler(myexception.class) public object handle(myexception e){ e.printstacktrace(); hashmap<string, object> map = new hashmap<>(); map.put("location", "myexception"); map.put("msg", e.getmessage()); map.put("localmsg", e.getlocalizedmessage()); return map; } @exceptionhandler(wantexception.class) public object handle(wantexception e){ e.printstacktrace(); hashmap<string, object> map = new hashmap<>(); map.put("location", "want-to-exception"); map.put("msg", e.getmessage()); map.put("localmsg", e.getlocalizedmessage()); return map; } }
调试源码可见匹配顺序为:异常层级高者优先!再清楚点,子类异常处理器优先
源码:
2. 存在不同的类中
源码可见
通过以上代码可以看到与多个异常处理类放入linkedhashmap的顺序有关,那么,这个放入的顺序是什么呢?继续看源码:
此处对异常处理类进行了排序,排序逻辑继续源码:
此处看到可以利用order指定顺序,如果没有,则默认最小顺序;
那么,如果都没有指定顺序的话,那就是list中的顺序,源码:
获取所有的beandefinitionnames,再遍历寻找标注了异常处理注解的类,放入list中(存在父容器的合并后再遍历寻找)
。。。待更新
到此这篇关于springboot全局异常处理之多个处理器匹配顺序的文章就介绍到这了,更多相关springboot多个处理器匹配顺序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论