REST Top

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

2. Mapping Endpoints

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 Swagger library.

3. Event Listener Approach

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.

3.1. ApplicationListener Interface

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.

3.2. @EventListener Annotation

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));
}

4. Actuator Approach

A second approach for retrieving a list of all our endpoints is via the Spring Boot Actuator feature.

4.1. Maven Dependency

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>

4.2. Configuration

When we add the spring-boot-actuator dependency, only /health and /info endpoints are available by default. 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.

5. Swagger

The Swagger library can also be used to list all endpoints of a REST API.

5.1. Maven Dependency

To add it to our project, we need a springfox-boot-starter dependency in the pom.xml file:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

5.2. Configuration

Let's create the configuration class by defining the Docket bean:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
      .select()
      .apis(RequestHandlerSelectors.any())
      .paths(PathSelectors.any())
      .build();
}

The Docket is a builder class that configures the generation of Swagger documentation. To access the REST API endpoints, we can visit this URL in our browser:

http://host/v2/api-docs

6. Conclusion

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 Swagger library.

As usual, all code samples used in this tutorial are available over on GitHub.

REST bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course :

>> CHECK OUT THE COURSE
Comments are closed on this article!