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 SpringDoc 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

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.

5. SpringDoc

The SpringDoc 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 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>

5.2. Configuration

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

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

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


« 上一篇: 在Java中解码JWT令牌
» 下一篇: Java Weekly, 第377期