23

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Spring Monkey
  • 4,988
  • 7
  • 32
  • 34

3 Answers3

19

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

Sida Zhou
  • 3,529
  • 2
  • 33
  • 48
  • 3
    it is worth noting that if you can't see the mappings tab when running your spring boot app then you probably want to do the following: 1- Enable MVC Spring plugin (if it is not already enabled), 2- Add actuator to your dependencies – Sul Aga Aug 31 '19 at 05:57
  • https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator maven dependency for the actuator – Kosi Oct 23 '19 at 13:54
  • 1
    Is there any way to print out the routes with the command line? – robskrob Sep 11 '21 at 18:38
  • @robskrob i had the same question. did u find an answer ? maybe a gradle task? – Sandeep Oct 07 '22 at 11:33
  • @Sandeep I did not find an answer to this, unfortunately. – robskrob Oct 10 '22 at 20:19
17

If you set the Log4J category for log4j.logger.org.springframework.web to INFO or DEBUG you should see the list of mappings in your server's log (e.g. catalina.out) when your app starts up.

For example:

INFO: DefaultAnnotationHandlerMapping: Mapped URL path [/about] onto handler [org.bozos.songfight.webapp.spring.controller.RootController@6bc947]
INFO: DefaultAnnotationHandlerMapping: Mapped URL path [/about.*] onto handler [org.bozos.songfight.webapp.spring.controller.RootController@6bc947]
INFO: DefaultAnnotationHandlerMapping: Mapped URL path [/about/] onto handler [org.bozos.songfight.webapp.spring.controller.RootController@6bc947]
...
INFO: SimpleUrlHandlerMapping: Mapped URL path [/login] onto handler [org.springframework.web.servlet.mvc.UrlFilenameViewController@4035acf6]
nbro
  • 15,395
  • 32
  • 113
  • 196
sdouglass
  • 2,350
  • 16
  • 25
3

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

WinDnDusT
  • 49
  • 3