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

作者:情陌人灬已不在 时间:2021-08-29 22:28:34 

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。

来源:https://www.cnblogs.com/deityjian/p/12533302.html

标签:SpringBoot,接口,路由
0
投稿

猜你喜欢

  • SpringBoot 嵌入式web容器的启动原理详解

    2021-12-29 23:23:14
  • SpringDataJPA在Entity中常用的注解介绍

    2023-11-27 09:40:36
  • Java毕业设计实战之校园一卡通系统的实现

    2022-11-26 06:32:56
  • 浅谈java安全编码指南之死锁dead lock

    2023-06-22 04:49:19
  • J2ee 高并发情况下监听器实例详解

    2022-12-26 05:47:43
  • C# 通过ServiceStack 操作Redis

    2023-12-13 06:18:01
  • Java中JMM与volatile关键字的学习

    2022-03-24 00:12:00
  • C# XmlDocument操作XML案例详解

    2021-11-23 04:00:01
  • Java运行时环境之ClassLoader类加载机制详解

    2022-07-18 04:54:05
  • Java设计模式之命令模式(Command模式)介绍

    2021-12-02 01:01:02
  • Android中TimePicker与DatePicker时间日期选择组件的使用实例

    2023-08-07 01:35:15
  • MyBatis动态SQL表达式详解

    2023-11-29 00:42:36
  • Java基础知识之CharArrayReader流的使用

    2023-02-12 10:40:29
  • 详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

    2021-11-18 12:18:05
  • C#下载歌词文件的同步和异步方法

    2023-04-11 22:46:49
  • c#使用windows服务更新站点地图的详细示例

    2021-07-24 10:45:52
  • Java中常见的对象转换工具

    2023-12-14 19:23:16
  • flutter实现appbar下选项卡切换

    2023-06-21 13:35:24
  • 关于springboot响应式编程整合webFlux的问题

    2023-12-07 07:25:55
  • java实现纸牌游戏之小猫钓鱼算法

    2021-08-11 22:57:00
  • asp之家 软件编程 m.aspxhome.com