Spring Boot 2 Actuator endpoints inaccessible with Jersey

南笙酒味 提交于 2019-12-10 18:21:29

问题


I have a very simple demo application using the Spring Boot 2.0.x.RELEASE

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;
    }
}

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?


回答1:


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.



来源:https://stackoverflow.com/questions/51620437/spring-boot-2-actuator-endpoints-inaccessible-with-jersey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!