Spring mvc(3)如何获取所有的requestMapping

来源:互联网 发布:淘宝店铺淘金币签到 编辑:程序博客网 时间:2024/06/07 02:09

Java 代码

@AutowiredApplicationContext context;private Map<Object, Object> requestMappings() {        Map<String, Object> map = context.getBeansWithAnnotation(Controller.class);        Map<Object, Object> mappingsMap = new LinkedHashMap<>();        for (Map.Entry<String, Object> entry : map.entrySet()) {            String key = entry.getKey();            Object value = entry.getValue();            String[] classMappingStrs = getReqMappingValues(value);            ArrayList<String[]> list = new ArrayList<>();            for (Method m : value.getClass().getMethods()) {                String[] methodStrs = getReqMappingValuesFromMethod(m);                if (!Util.isEmpty(methodStrs)) {                    list.add(methodStrs);                }            }            for (String str : classMappingStrs) {                for (String[] ss : list) {                    for (String s : ss) {                        mappingsMap.put(str+s, key);                    }                }            }            LogCore.BASE.info("look controller key= {}, v{}", key, value.getClass().getName());        }        return mappingsMap;    }
private String[] getReqMappingValues(Object value) {        String[] v3 = Util.toNullDefalut(value.getClass().getAnnotation(RequestMapping.class), RequestMapping::value,                new String[]{""});        return v3;    }    private String[] getReqMappingValuesFromMethod(Method method) {        String[] v3 = Util.toNullDefalut(method.getAnnotation(RequestMapping.class), RequestMapping::value, new String[]{});        return v3;    }
    /**     * @param s     * @param func s如果不为null,则执行此表达式     * @param nullDefalut 如果s为null直接返回这个值     */    public static <T,R> R toNullDefalut(T t,Function<T, R> func, R nullDefalut){        if(null == t){            return nullDefalut;        }        return func.apply(t);    }

这里写图片描述