简体   繁体   中英

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. Therefore I use the @RestEndpointController annotation to get "full" control over the endpoints via MVC annotations. But I recognized a problem when I use the @RequestMapping annotation on class level on my RestEndpointController class.

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

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

and my 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

  • /actuator/tenant/customer/{id}
  • /customer/{id}

How can I avoid the second (non actuator) endpoint to be registered? Or is it a bug of Actuator / Spring-Boot?

Do not use @RequestMapping on class level. You can mention the subsequent path as part of @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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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