javaspringkotlinspring-webfluxspring-actuator

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


I wonder if there's a way to get all configured mappings in runtime without using Actuator's MappingsEndpoint.

With MappingsEndpoint it's as easy as pie (in Kotlin notation, in Java it's basically the same):

       @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()
           ...

However it makes use of Actuator and not all setups permit the MappingsEndpoint bean.

I wonder if it's possible to somehow query configured routes for the whole server from within Java/Kotlin code.


Solution

  • I think you can use RequestMappingHandlerMapping get to a full list of the registered mappings.

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

    This will give you a list of Strings that look like,

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

    etc