温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-09-13 07:29:54 来源:亿速云 阅读:393 作者:chen 栏目:开发技术

本篇内容主要讲解“如何使用SpringBoot获取所有接口的路由”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用SpringBoot获取所有接口的路由”吧!

目录
  • 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。

到此,相信大家对“如何使用SpringBoot获取所有接口的路由”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI