
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: March 26, 2025
When working with a REST API, it’s common to retrieve all of the REST endpoints. For example, we might need to save all request mapping endpoints in a database. In this tutorial, we’ll look at how to get all the REST endpoints in a Spring Boot application.
In a Spring Boot application, we expose a REST API endpoint by using the @RequestMapping annotation in the controller class. For getting these endpoints, there are three options: an event listener, Spring Boot Actuator, or the SpringDoc library.
For creating a REST API service, we use @RestController and @RequestMapping in the controller class. These classes register in the spring application context as a spring bean. Therefore, we can get the endpoints by using the event listener when the application context is ready at startup. There are two ways to define a listener. We can either implement the ApplicationListener interface or use the @EventListener annotation.
When implementing the ApplicationListener, we must define the onApplicationEvent() method:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping
.getHandlerMethods();
map.forEach((key, value) -> LOGGER.info("{} {}", key, value));
}
In this way, we use the ContextRefreshedEvent class. This event is published when the ApplicationContext is either initialized or refreshed. Spring Boot provides many HandlerMapping implementations. Among these is the RequestMappingHandlerMapping class, which detects request mappings and is used by the @RequestMapping annotation. Therefore, we use this bean in the ContextRefreshedEvent event.
The other way to map our endpoints is to use the @EventListener annotation. We use this annotation directly on the method that handles the ContextRefreshedEvent:
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping
.getHandlerMethods();
map.forEach((key, value) -> LOGGER.info("{} {}", key, value));
}
A second approach for retrieving a list of all our endpoints is via the Spring Boot Actuator feature.
For enabling this feature, we’ll add the spring-boot-actuator Maven dependency to our pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Only /health and /info endpoints are available by default when we add the spring-boot-actuator dependency. To enable all the actuator endpoints, we can expose them by adding a property to our application.properties file:
management.endpoints.web.exposure.include=*
Or, we can simply expose the endpoint for retrieving the mappings:
management.endpoints.web.exposure.include=mappings
Once enabled, the REST API endpoints of our application are available at http://host/actuator/mappings.
The SpringDoc library can also be used to list all endpoints of a REST API.
To add it to our project, we need a springdoc-openapi-ui dependency in the pom.xml file:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
Let’s create the configuration class by defining the OpenAPI bean:
@Bean
public OpenAPI openAPI() {
return new OpenAPI().info(new Info().title("SpringDoc example")
.description("SpringDoc application")
.version("v0.0.1"));
}
To access the REST API endpoints, we can visit this URL in our browser:
http://localhost:8080/swagger-ui/index.html
In this article, we describe how to retrieve request mapping endpoints in a Spring Boot application by using the Event listener, Spring Boot Actuator, and SpringDoc library.