Spring Boot 2 Actuator endpoints inaccessible with Jersey

Tim :

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?

Paul Samsotha :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103940&siteId=1