简体   繁体   English

Spring 引导执行器自定义 RestControllerEndpoint 与 class 级别的 RequestMapping 注释

[英]Spring Boot Actuator Custom RestControllerEndpoint with RequestMapping Annotation on class level

I have some custom actuator endpoints which should have the same parameters after the endpoint id.我有一些自定义执行器端点,它们应该在端点 id 之后具有相同的参数。 Therefore I use the @RestEndpointController annotation to get "full" control over the endpoints via MVC annotations.因此,我使用@RestEndpointController注释通过 MVC 注释来“完全”控制端点。 But I recognized a problem when I use the @RequestMapping annotation on class level on my RestEndpointController class.但是当我在我的 RestEndpointController class 上的 class 级别上使用@RequestMapping注释时,我发现了一个问题。

My sample implementation:我的示例实现:

@Component
@RestControllerEndpoint(id = "tenant")
@RequestMapping("customer/{id}")
public class TenantEndpoint {

    @GetMapping
    public String getTenantById(@PathVariable("id") String customerId) {
        return "Tenant_" + customerId;
    }
}

with the following application.properties使用以下 application.properties

management.endpoints.web.exposure.include=health, info, metrics, tenant

and my pom.xml和我的 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>

when running this app, there are two endpoints registered in Spring Boot运行此应用程序时,在 Spring Boot 中注册了两个端点

  • /actuator/tenant/customer/{id} /执行器/租户/客户/{id}
  • /customer/{id} /客户ID}

How can I avoid the second (non actuator) endpoint to be registered?如何避免注册第二个(非执行器)端点? Or is it a bug of Actuator / Spring-Boot?还是 Actuator / Spring-Boot 的错误?

Do not use @RequestMapping on class level.不要在 class 级别上使用@RequestMapping You can mention the subsequent path as part of @GetMapping .您可以将后续路径作为@GetMapping的一部分提及。 Below code will register only a single endpoint viz.下面的代码将只注册一个端点,即。 /actuator/tenant/customer/{id}

@Component
@RestControllerEndpoint(id = "tenant")
public class TenantEndpoint {

    @GetMapping("/customer/{id}")
    public String getTenantById(@PathVariable("id") String customerId) {
        return "Tenant_" + customerId;
    }
}

you should try, I didn't check it, bring some feedback in case you test it please.你应该试试,我没有检查它,如果你测试它,请提供一些反馈。

@Component
@RestControllerEndpoint(id = "tenant")
@RequestMapping("customer")
public class TenantEndpoint {

    @GetMapping("/{id}")
    public String getTenantById(@PathVariable("id") String customerId) {
        return "Tenant_" + customerId;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM