How to show all available routes in Spring?

Viewed 19797

How to show all the routes mapped in a spring based application? In Rails this is done using rake routes.

I use two mapping methods of spring to create the URL-mappings:

  • @RequestMapping
  • SimpleUrlHandler

I have used the Unix command grep and cut to get all the mappings of @RequestMapping. I wonder if there is some way I can get these details from the Spring application.

3 Answers

If you are using Intellij (ultimate edition), then after you build/run the project, you can view the routes in the bottom toolbar Run -> Endpoints -> Mappings.

enter image description here

Solution

This can be done with spring-boot-starter-web:2.6.5 which makes use of spring-webmvc:5.3.17.
Add this config to your application.yml (or similar):

logging:
  level:
    _org.springframework.web.servlet.HandlerMapping.Mappings: debug

The underscore '_' in the logger name is important.
This property will generate logs during application boot that look like this example of logs from spring-webmvc:5.3.17's AbstractHandlerMapping.class

How it works

The property references spring-webmvc:5.3.17's org.springframework.web.servlet.handler.AbstractHandlerMapping which creates a hidden logger for mappings spring-webmvc:5.3.17's AbstractHandlerMapping.class logger declaration

This logger gets used in method detectHandlerMethods() of subclass AbstractHandlerMethodMapping spring-webmvc:5.3.17's AbstractHandlerMethodMapping.class

It looks like this technique goes back as early as spring-webmvc 5.3.5 (march 2021):
Github Spring Framework Release 5.3.5

Here's the commit containing the changes:
Github Spring Framework commit with changes for logging routes

Related