我有一个使用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,则可以执行器端点。我必须添加一些配置以允许执行器端点通过运动衫吗?
答案 0 :(得分:2)
这是因为默认情况下,Jersey将使用URL映射/*
,该映射将占用所有请求,包括对执行器端点的请求,而找不到。有两种解决方案;您可以将Jersey的基本网址更改为其他名称,例如/api/*
,也可以将Jersey设置为过滤器(而不是默认的servlet),并设置一个属性以使Jersey将不知道的所有请求转发到servlet容器。这两个示例都可以在this post中找到。