有没有办法在没有执行器的情况下获取 WebFlux 中所有配置的路由?

Is there a way to get all configured routes in WebFlux without actuator?

我想知道是否有一种方法可以在不使用 Actuator 的情况下在运行时获取所有配置的映射 MappingsEndpoint

使用 MappingsEndpoint 就像馅饼一样简单(在 Kotlin 符号中,在 Java 中基本相同):

       @Autowired val mappingsEndpoint: org.springframework.boot.actuate.web.mappings.MappingsEndpoint
       mappingsEndpoint.mappings().contexts.values
           .asSequence()
           .mapNotNull { it.mappings["dispatcherHandlers"] }
           .filterIsInstance<Map<*, *>>()
           .mapNotNull { it["webHandler"] as? List<*> }
           .flatten()
           ...

但是它使用了 Actuator,并不是所有设置都允许 MappingsEndpoint bean。

我想知道是否可以从 Java/Kotlin 代码中以某种方式查询整个服务器的已配置路由。

我认为您可以使用 RequestMappingHandlerMapping 获取已注册映射的完整列表。

@Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping;

...

Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();

List<String> mappings = handlerMethods.keySet().stream().map((handlerMethod -> handlerMethod.getMethodsCondition() + ":" + handlerMethod.getPathPatternsCondition())).collect(Collectors.toList());

这会给你一个字符串列表,看起来像,

[PUT]:[/endpoint]
[GET]:[/endpoint]

等等