我有一些自定义执行器端点,它们在端点 id 之后应该具有相同的参数。因此,我使用 @RestEndpointController 注释通过 MVC 注释获得对端点的“完全”控制。但当我在 RestEndpointController 类上的类级别使用 @RequestMapping
注释时,我发现了一个问题。
我的示例实现:
@Component
@RestControllerEndpoint(id = "tenant")
@RequestMapping("customer/{id}")
public class TenantEndpoint {
@GetMapping
public String getTenantById(@PathVariable("id") String customerId) {
return "Tenant_" + customerId;
}
}
具有以下 application.properties
management.endpoints.web.exposure.include=health, info, metrics, tenant
和我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
运行此应用程序时,Spring Boot 中注册了两个端点
- /actuator/tenant/customer/{id}
- /客户/{id}
如何避免注册第二个(非执行器)端点? 或者是 Actuator/Spring-Boot 的 bug?
最佳答案
不要在类级别使用@RequestMapping
。您可以将后续路径作为 @GetMapping
的一部分提及。下面的代码将仅注册一个端点即。 /actuator/tenant/customer/{id}
@Component
@RestControllerEndpoint(id = "tenant")
public class TenantEndpoint {
@GetMapping("/customer/{id}")
public String getTenantById(@PathVariable("id") String customerId) {
return "Tenant_" + customerId;
}
}
关于java - Spring Boot Actuator 自定义 RestControllerEndpoint 与类级别的 RequestMapping 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58972823/