泽西岛无法访问Spring Boot 2 Actuator端点 [英] Spring Boot 2 Actuator endpoints inaccessible with Jersey
问题描述
我有一个使用Spring Boot 2.0.x.RELEASE的非常简单的演示应用程序
I have a very simple demo application using the Spring Boot 2.0.x.RELEASE
在我的POM中,我有:
In my POM I have:
<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>
我的主应用程序看起来像:
My main application just looks like:
@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,则可以执行器端点.我必须添加一些配置以允许执行器端点通过运动衫吗?
When I then try and hit an actuator endpoint such as /actuator
I receive a 404, but I can hit the endpoints in TestEndpoints.java
with no problem. I had this working with Spring Boot 1.5.x, but now it seems that Jersey is not allowing the actuator endpoints through. If I remove the ResourceConfig bean completely, then I can hit the actuator endpoints. Is there some configuration I have to add to allow the actuator endpoints through jersey?
推荐答案
这是因为默认情况下,Jersey将使用url映射/*
,它将映射所有找不到的请求,包括到执行器端点的请求. .有两种解决方案;您可以将Jersey的基本网址更改为其他名称,例如/api/*
,或者您可以将Jersey设置为过滤器(而不是默认的servlet),并设置属性以使Jersey将该未知的所有请求转发到servlet容器.这两个示例都可以在这篇文章中找到.
It's because by default Jersey will use the url mapping /*
, which will hog up all requests, including those to the actuator endpoints, which it will not find. There are two solutions; you can either change the base URL for Jersey to something else, e.g. /api/*
or you can configure Jersey as a filter (instead of the default servlet) and set a property to make Jersey forward all requests it doesn't know down to the servlet container. Both examples can be found in this post.
这篇关于泽西岛无法访问Spring Boot 2 Actuator端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!