1

Is there a way to get a list of all the endpoints declared in a @RestController annotated class? With some patience that could be achieved via reflections but is there any Spring built-in way for that? Idea is to show this list on a service landing page.

S. Pauk
  • 5,208
  • 4
  • 31
  • 41
  • Have you looked at swagger.io? It does what you want to do. – whistling_marmot Jan 23 '16 at 18:02
  • @whistling_marmot looks interesting but it requires too many additional annotations for bottom-top approach to work. I was looking for something that will not pollute my code at all. – S. Pauk Jan 23 '16 at 18:12

1 Answers1

4

A registry of all the handler mappings is kept in the requestMappingHandlerMapping bean. You can access it from your Spring Boot Application's main method like this.

public static void main(String[] args) {
    ConfigurableApplicationContext context = 
            SpringApplication.run(MySpringApplication.class, args);

    AbstractHandlerMethodMapping requestMappingHandlerMapping = 
            context.getBean("requestMappingHandlerMapping", AbstractHandlerMethodMapping.class);
    Map handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
    System.out.println("handlerMethods: " + handlerMethods);
}

Or just autowire the bean into whichever of your Spring beans you need it in.

whistling_marmot
  • 3,561
  • 3
  • 25
  • 39