SpringBoot获取接口路由 使用SpringBoot获取所有接口的路由

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot获取接口路由 使用SpringBoot获取所有接口的路由

情陌人灬已不在   2021-09-11 我要评论
想了解使用SpringBoot获取所有接口的路由的相关内容吗,情陌人灬已不在在本文为您仔细讲解SpringBoot获取接口路由的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot获取接口,SpringBoot路由,接口的路由,下面大家一起来学习吧。

SpringBoot获取所有接口的路由

@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);
        }

Springboot部分路由生效

问题记录

项目新增接口"foo",始终不生效,经排查发现controller层的@RequestMaping(value=“test”)统一加了基础路径"test",我新增的接口注解为@PostMappinp(“test/foo),导致生成的路由为"test/test/foo”, 调用地址为"test/foo",所以报了404。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们