【问题标题】:Spring Boot 2 Actuator endpoints inaccessible with JerseyJersey 无法访问 Spring Boot 2 Actuator 端点
【发布时间】:2019-01-08 06:23:57
【问题描述】:

我有一个使用 Spring Boot 2.0.x.RELEASE 的非常简单的演示应用程序

在我的 POM 中有:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
        <exclusions>
            <exclusion>
                <!-- We're using undertow as our embedded web container instead of tomcat -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Embedded web container- serves Jersey resources -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>

    <!-- Support for Spring Actuator & Health Check Endpoints -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

我的主应用程序看起来像:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    ResourceConfig getJerseyConfig() {
        final HashMap<String, Object> jerseyProperties = new HashMap<>();
        jerseyProperties.put(ServerProperties.MEDIA_TYPE_MAPPINGS, "json : application/json");
        ResourceConfig resourceConfig = new ResourceConfig()
                .register(TestEndpoints.class);
        resourceConfig = resourceConfig.setProperties(jerseyProperties);

        return resourceConfig;
    }
}

当我尝试点击执行器端点(例如/actuator)时,我收到 404,但我可以毫无问题地点击TestEndpoints.java 中的端点。我使用 Spring Boot 1.5.x 进行了此操作,但现在看来 Jersey 不允许执行器端点通过。如果我完全删除 ResourceConfig bean,那么我可以点击执行器端点。我必须添加一些配置以允许执行器端点通过球衣吗?

【问题讨论】:

标签: java spring-boot jersey-2.0 spring-boot-actuator


【解决方案1】:

这是因为默认情况下,Jersey 将使用 url 映射 /*,这将占用所有请求,包括到执行器端点的请求,它不会找到。有两种解决方案;您可以将 Jersey 的基本 URL 更改为其他内容,例如/api/* 或者您可以将 Jersey 配置为过滤器(而不是默认的 servlet)并设置一个属性以使 Jersey 将它不知道的所有请求转发到 servlet 容器。这两个例子都可以在this post找到。

【讨论】:

  • 谢谢,添加成功。在升级到 SB 2/Finchley 之前,我是否有理由在不需要过滤器和转发属性的情况下访问执行器?
  • 我不知道。这一直是个问题,甚至在 2.0 之前也是如此。您是否使用了不同的基本 URI?
  • 这是在我之前设置的,所以不确定他们是如何在没有这个的情况下让它工作的。但一切都在 //v/* 和 //v/management/* 用于执行器。这与我现在使用的设置相同,只是更新了所有内容,所以我看不出它以前是如何工作的。感谢您的帮助
猜你喜欢
  • 2016-06-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2016-03-27
  • 2018-03-26
  • 2021-10-14
  • 2016-09-07
相关资源
最近更新 更多