1. Overview
1.概述
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.
在使用REST API时,检索所有的REST端点是很常见的。例如,我们可能需要在一个数据库中保存所有的请求映射端点。在本教程中,我们将探讨如何在Spring Boot应用程序中获取所有REST端点。
2. Mapping Endpoints
2.映射端点
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.
在Spring Boot应用程序中,我们通过在控制器类中使用@RequestMapping注解来公开REST API端点。要获得这些端点,有三种选择:事件监听器、Spring Boot Actuator或Swagger库。
3. Event Listener Approach
3.事件监听器方法
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.
为了创建一个REST API服务,我们在控制器类中使用@RestController和@RequestMapping。这些类在spring应用上下文中作为spring bean注册。因此,当应用上下文在启动时准备好时,我们可以通过使用事件监听器获得端点。有两种方法来定义监听器。我们可以实现ApplicationListener接口,或者使用@EventListener注释。
3.1. ApplicationListener Interface
3.1.ApplicationListener接口
When implementing the ApplicationListener, we must define the onApplicationEvent() method:
在实现ApplicationListener时,我们必须定义onApplicationEvent()方法。
@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.
通过这种方式,我们使用ContextRefreshedEvent类。该事件在ApplicationContext被初始化或刷新时发布。Spring Boot提供了许多HandlerMapping实现。其中有RequestMappingHandlerMapping类,它可以检测请求映射,并被@RequestMapping注解使用。因此,我们在ContextRefreshedEvent事件中使用这个Bean。
3.2. @EventListener Annotation
3.2.@EventListener 注释
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注解。我们直接在处理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
4.执行器方法
A second approach for retrieving a list of all our endpoints is via the Spring Boot Actuator feature.
检索我们所有端点列表的第二个方法是通过Spring Boot Actuator功能。
4.1. Maven Dependency
4.1.Maven的依赖性
For enabling this feature, we’ll add the spring-boot-actuator Maven dependency to our pom.xml file:
为了启用该功能,我们将在pom.xml文件中添加spring-boot-actuator Maven依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.2. Configuration
4.2.配置
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:
当我们添加spring-boot-actuator依赖时,默认情况下只有/health和/info端点可用。要启用所有执行器端点,我们可以通过向application.properties文件添加一个属性来公开它们。
management.endpoints.web.exposure.include=*
Or, we can simply expose the endpoint for retrieving the mappings:
或者,我们可以简单地暴露用于检索mappings的端点。
management.endpoints.web.exposure.include=mappings
Once enabled, the REST API endpoints of our application are available at http://host/actuator/mappings.
一旦启用,我们应用程序的REST API端点就可以在http://host/actuator/mappings。
5. Swagger
5.斯瓦格
The Swagger library can also be used to list all endpoints of a REST API.
Swagger库也可用来列出REST API的所有端点。
5.1. Maven Dependency
5.1.Maven的依赖性
To add it to our project, we need a springfox-boot-starter dependency in the pom.xml file:
为了将其添加到我们的项目中,我们需要在pom.xml文件中加入springfox-boot-starter依赖性。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
5.2. Configuration
5.2.配置
Let’s create the configuration class by defining the Docket bean:
让我们通过定义Docketbean来创建配置类。
@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:
Docket是一个构建器类,用于配置Swagger文档的生成。要访问REST API端点,我们可以在浏览器中访问这个URL。
http://host/v2/api-docs
6. Conclusion
6.结语
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.
在这篇文章中,我们描述了如何通过使用事件监听器、Spring Boot Actuator和Swagger库,在Spring Boot应用程序中检索请求映射端点。
As usual, all code samples used in this tutorial are available over on GitHub.
像往常一样,本教程中使用的所有代码样本都可以在GitHub上找到。。