zoukankan      html  css  js  c++  java
  • SpringMVC项目中获取所有URL到Controller Method的映射

        /**
         * 获取所有的url
         * 
         * @author Lynch
         */
        @ResponseBody
        @RequestMapping("index")
        public void index() {
            List<RestModel> list = new ArrayList<RestModel>();
            Map<String, RestModel> map = new HashMap<String, RestModel>();
            List<String> urlList = new ArrayList<String>();
            WebApplicationContext wac = (WebApplicationContext) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            Map<String, HandlerMapping> requestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(wac, HandlerMapping.class, true, false);
            for (HandlerMapping handlerMapping : requestMappings.values()) {
                if (handlerMapping instanceof RequestMappingHandlerMapping) {
                    RequestMappingHandlerMapping rmhm = (RequestMappingHandlerMapping) handlerMapping;
                    Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
                    for (RequestMappingInfo rmi : handlerMethods.keySet()) {
                        PatternsRequestCondition prc = rmi.getPatternsCondition();
                        RequestMethodsRequestCondition mt  = rmi.getMethodsCondition(); 
                        String md = "";
                        for(RequestMethod method : mt.getMethods()) {
                            md = method.name();
                            break;
                        }
                        Set<String> patterns = prc.getPatterns();
                        HandlerMethod handlerMethod = handlerMethods.get(rmi);
                        for (String url : patterns) {  
                            Class<?> clazz = handlerMethod.getBeanType();
                            Method method = handlerMethod.getMethod();
                            RestModel restModel = new RestModel();
                            restModel.setUrl(url);
                            restModel.setClazz(clazz.toString());
                            restModel.setMethod(md);
                            map.put(url, restModel);
                            urlList.add(url);
                        }
                    }
                }
            }
            String[] urls = new String[urlList.size()];
            urls = urlList.toArray(urls);
            Arrays.sort(urls);
            for (String url : urls) {
                list.add(map.get(url));
            }
     
        }
  • 相关阅读:
    14 用DFT计算线性卷积
    13 DFT变换的性质
    12 有限长序列的分类
    前端常见跨域解决方案
    JS基础-构造函数、原型、原型链、继承
    浏览器中的 JavaScript 执行机制
    再也不怕面试官问我 Promise 相关问题了
    前端面试---手写代码常考题
    从输入 URL 到页面展示,这中间发生了什么?
    学习笔记-CSS-圣杯布局和双飞翼布局
  • 原文地址:https://www.cnblogs.com/linjiqin/p/11059393.html
Copyright © 2011-2022 走看看