I have a rest service written with spring boot. I want to get all endpoints after start up. How can i achieve that? Purpose of this, i want to save all endpoints to a db after start up (if they are not already exist) and use these for authorization. These entries will be inject into roles and roles will be used to create tokens.
-
You should post some code and explain what you have tried – jhhoff02 Apr 21 '17 at 17:20
-
1Possible duplicate of https://stackoverflow.com/questions/32525699/listing-all-deployed-rest-endpoints-spring-boot-jersey – Ilya Serbis May 15 '18 at 11:12
5 Answers
You can get RequestMappingHandlerMapping at the start of the application context.
@Component
public class EndpointsListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods()
.forEach(/*Write your code here */);
}
}
Alternately you can also Spring boot actuator(You can also use actutator even though you are not using Spring boot) which expose another endpoint(mappings endpoint) which lists all endpoints in json. You can hit this endpoint and parse the json to get the list of endpoints.

- 3,253
- 2
- 33
- 55

- 3,434
- 1
- 28
- 34
-
hi, thanks for answer. i know about /mappings endpoint but it is not useful for my scenario. i want my services to load their endpoints a table at startup. /mappings serves endpoint list to any other requester. i will try your code fragment. – barbakini Apr 21 '17 at 13:08
-
You can use a resttemplate to call the /mappings endpoint on ApplicationListener.onApplicationEvent() and get the endpoints and save it to the database. – Praneeth Ramesh Apr 21 '17 at 13:18
-
if i write a central resource listener service to gather endpoints from all microservices, i can use /mappings. otherwise it seems a bit bizarre to me – barbakini Apr 21 '17 at 13:20
-
1I had to add `@Component` to the class from `import org.springframework.stereotype.Component;` to get this to work. – Joe Bane Aug 02 '19 at 19:05
You need 3 steps to exposure all endpoints:
- enable Spring Boot Actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- enable endpoints
In Spring Boot 2, Actuator comes with most endpoints disabled, the only 2 available by default are :
/health
/info
If you want to enable all of the endpoints, just set:
management.endpoints.web.exposure.include=*
For more details, refer to:
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
- go!
btw, In Spring Boot 2, Actuator simplifies its security model by merging it with the application one.
For more details, refer to this article:

- 1,316
- 15
- 19
-
2You can find the endpoint, handler etc under /actuator/mappings (Springboot 2) – abasar Apr 17 '20 at 05:58
-
It returns a json. Any way to pretty print that like `rake routes` in Ruby on Rails? – wlnirvana Apr 20 '21 at 00:39
As an addition to the above comments, since Spring 4.2 you may use the @EventListener
annotation like this:
@Component
public class EndpointsListener {
private static final Logger LOGGER = LoggerFactory.getLogger(EndpointsListener.class);
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
applicationContext.getBean(RequestMappingHandlerMapping.class)
.getHandlerMethods().forEach((key, value) -> LOGGER.info("{} {}", key, value));
}
}
If you want to find out more about how to use the Spring Events and to create custom events, please check out this article: Spring Events

- 587
- 7
- 22
In the application.properties, we need management.endpoints.web.exposure.include=mappings
Then we can see all the endpoints at: http://localhost:8080/actuator/mappings
Don't forget to add the actuator to the POM.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Late to the party but you can directly use
@Autowired
private RequestMappingHandlerMapping requestHandlerMapping;
this.requestHandlerMapping.getHandlerMethods()
.forEach((key, value) -> /* whatever */));

- 319
- 1
- 7
-
This solution does not seem to guarantee that requestHandlerMapping is already fully populated. – Doron Gold Apr 07 '20 at 10:31