当前位置: 首页 > 知识库问答 >
问题:

Spring Boot Actuator自定义RestControlllerEndpoint,在类级别具有Request estMap注释

沈皓君
2023-03-14

我有一些自定义执行器endpoint,它们应该在endpointid之后具有相同的参数。因此,我使用@RestEndpoint Controller注释通过MVC注释获得对endpoint的“完全”控制。但是当我在我的RestEndpoint Controller类的类级别上使用@Request estMap注释时,我发现了一个问题。

我的示例实现:

@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>

运行此应用程序时,有两个endpoint在Spring Boot中注册

  • /执行器/租户/客户/{id}
  • /客户/{id}

如何避免注册第二个(非执行器)endpoint?或者是执行器/Spring靴的缺陷?

共有2个答案

白博易
2023-03-14

你应该试试,我没有检查它,请带来一些反馈,以防你测试它。

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

    @GetMapping("/{id}")
    public String getTenantById(@PathVariable("id") String customerId) {
        return "Tenant_" + customerId;
    }
}
戈博易
2023-03-14

不要在类级别使用@Request estMap。您可以将后续路径作为@GetMap的一部分提及。下面的代码将只注册一个endpoint,即。/执行器/租户/客户/{id}

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

    @GetMapping("/customer/{id}")
    public String getTenantById(@PathVariable("id") String customerId) {
        return "Tenant_" + customerId;
    }
}
 类似资料:
  • 我有一个元注释,它注释了我的测试类: 而我的测试类看起来是: 然后我得到了这个异常:的多个声明详细信息:

  • 我正在使用高级自定义字段为特定类别创建帖子。我使用Elementor(免费版)进行格式化,我希望使用ACF创建的所有帖子都应该显示在我的页面的特定部分下。但是,我不能这样做,因为elementor pro支持此功能,并且我不能用elementor编写php。 知道怎么做吗?

  • 我想按字母顺序对字段名列表进行排序,但是我需要在比较器的doCompare方法中包含一个条件,以便如果字段名是“pk”,则始终将其排序到列表的顶部。我所拥有的内容如下,但我不确定我是否采取了正确的方法,特别是reurn值为-1000。对此的任何建议都将不胜感激。

  • 我在我的wordpress网站上创建了一个自定义帖子类型“Portfolio”,还为这个自定义帖子类型创建了一个类别部分,其中包含类别slug“PortCate”。 “Portfolio”帖子的永久链接如下: mysite.com/portfolio/post-name/ 但是类别url是 mysite.com/port-cate/category-slug/ 我的问题是:如何为这种帖子类型创建分

  • 问题内容: 我正在尝试使用类级别的自定义注释来实现跨域验证(JSR-303)。但是,不会调用isValid方法(而是调用initialize方法)。 所以我的问题是:为什么不为该类级别的验证程序调用isValid方法?在属性级别定义它是可行的! 我在JBoss AS 7和Websphere AS 8上尝试过。 这是代码和一个JUnit测试(有效) Test.java DateCompare.jav

  • 问题内容: 如何在Go中创建只能接受有效值的自定义类型?例如,我要创建一个名为“名称”的类型,其基础类型为字符串。但是,它只能接受值“ John”,“ Rob”或“ Paul”。其他任何值都将返回错误。我已经以非常简单的方式创建了以下程序,以表示我想要实现的目标。 http://play.golang.org/p/jzZwALsiXz 编写此代码的最佳方法是什么? 问题答案: 您可以执行以下操作(