随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

springboot url中获取所有RequestMapping的URL路径列表集

spring boot 项目在做URL权限控制的时候需要获取全部的URL,一个一个去controller中找费时费力,有的权限点的命名和URL有一定的对应关系。如果能用程序获得全部URL,将会省去很多事。在项目中添加如下Controller,请求/getAllUrl,即可看到项目所有的URL。当然也可以根据项目将URL写入数据库或写入配置文件。

复制代码
    @Autowired
    WebApplicationContext applicationContext;
 
    @RequestMapping(value = "v1/getAllUrl", method = RequestMethod.POST)
    public Object getAllUrl() {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
        
//        List<String> urlList = new ArrayList<>();
//        for (RequestMappingInfo info : map.keySet()) {
//            // 获取url的Set集合,一个方法可能对应多个url
//            Set<String> patterns = info.getPatternsCondition().getPatterns();
//
//            for (String url : patterns) {
//                urlList.add(url);
//            }
//        }
 
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            Map<String, String> map1 = new HashMap<String, String>();
            RequestMappingInfo info = m.getKey();  
            HandlerMethod method = m.getValue();  
            PatternsRequestCondition p = info.getPatternsCondition();  
            for (String url : p.getPatterns()) {  
                map1.put("url", url);
            }  
            map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名  
            map1.put("method", method.getMethod().getName()); // 方法名 
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
            for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                map1.put("type", requestMethod.toString());
            }
            
            list.add(map1);
        }
 
        JSONArray jsonArray = JSONArray.fromObject(list);
 
        return jsonArray;
    }
复制代码
复制代码
        ApplicationContext context = event.getApplicationContext();
        
        RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
        for (Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            RequestMappingInfo info = m.getKey();  
            HandlerMethod method = m.getValue();  
            System.out.println("请求url:" + info.getPathPatternsCondition() );
            System.out.println("请求方式:" + info.getMethodsCondition());
            System.out.println("className:" + method.getMethod().getDeclaringClass().getName());
            System.out.println("method:" + method.getMethod().getName());
            System.out.println("===================");
        }
复制代码

 


posted on   Ruthless  阅读(2296)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2021-01-15 Springboot+RabbitMQ实战demo
2012-01-15 一、oracle 高水位线详解
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示