拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 怎么获取Spring Boot中的所有REST端点

怎么获取Spring Boot中的所有REST端点

白鹭 - 2021-11-24 374 0 0

1.概述

使用REST API时,通常会检索所有REST端点。例如,我们可能需要将所有请求映射终结点保存在数据库中。在本教程中,我们将研究如何在Spring Boot应用程序中获取所有REST端点。

2.映射端点

在Spring Boot应用程序中,我们通过使用控制器类中@RequestMapping为了获得这些端点,有三个选项:事件监听器,Spring Boot Actuator或Swagger库。

3.事件监听器方法

为了创建REST API服务,我们在控制器类中@RestController@RequestMapping这些类在spring应用程序上下文中注册为spring bean。因此,当应用程序上下文在启动时准备就绪时,我们可以使用事件侦听器获取端点。有两种定义侦听器的方法。我们可以实现ApplicationListener接口,也可以使用@ EventListener批注。

3.1。 ApplicationListener接口

在实现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));

 }

这样,我们使用了ContextRefreshedEvent类。初始化或刷新ApplicationContext时,将发布此事件。 Spring Boot提供了许多HandlerMapping实现。其中之一是RequestMappingHandlerMapping类,该类检测请求映射,并由@RequestMapping批注使用。因此,我们在ContextRefreshedEvent事件中使用此bean。

3.2 @EventListener注释

映射端点的另一种方法是使用@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方法

检索所有端点列表的第二种方法是通过Spring Boot Actuator功能。

4.1 Maven依赖

为了启用此功能,我们将·spring-boot-actuator· Maven依赖项添加到我们的pom.xml文件中:

<dependency>

 <groupId>org.springframework.boot</groupId>

 <artifactId>spring-boot-starter-actuator</artifactId>

 </dependency>

4.2 配置

当我们添加spring-boot-actuator依赖项时,默认情况下/health/info要启用所有执行器端点application.properties文件中添加一个属性来公开它们:

management.endpoints.web.exposure.include=*

或者,我们可以简单地公开端点以获取mappings

management.endpoints.web.exposure.include=mappings

http://host/actuator/mappings上获得我们应用程序的REST API端点。

5.Swagger

Swagger库还可用于列出REST API的所有端点。

5.1 Maven依赖

要将其添加到我们的项目中,我们需要pom.xml文件中springfox-boot-starter依赖项:

<dependency>

 <groupId>io.springfox</groupId>

 <artifactId>springfox-boot-starter</artifactId>

 <version>3.0.0</version>

 </dependency>

5.2配置

让我们通过定义Docket bean创建配置类:

@Bean

 public Docket api() {

 return new Docket(DocumentationType.SWAGGER_2)

 .select()

 .apis(RequestHandlerSelectors.any())

 .paths(PathSelectors.any())

 .build();

 }

Docket是一个构建器类,用于配置Swagger文档的生成。要访问REST API端点,我们可以在浏览器中访问以下URL:

http://host/v2/api-docs

六,结论

在本文中,我们描述了如何通过使用事件侦听器,Spring Boot Actuator和Swagger库在Spring Boot应用程序中检索请求映射端点。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *