3
votes

I am trying to upgrade a spring boot boilerplate project to Spring boot 2.0.0. I followed the official migration guide(this and this) but it is unable to expose the actuator custom endpoints.

I tested with this dummy endpoint:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Endpoint(id="testing-user")
public class ActiveUsersEndpoint {

private final Map<String, User> users = new HashMap<>();

ActiveUsersEndpoint() {
    this.users.put("A", new User("Abcd"));
    this.users.put("E", new User("Fghi"));
    this.users.put("J", new User("Klmn"));
}

@ReadOperation
public List getAll() {
    return new ArrayList(this.users.values());
}

@ReadOperation
public User getActiveUser(@Selector String user) {
    return this.users.get(user);
}

public static class User {
    private String name;

    User(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
}

The endpoints work well if exposed directly from the child project but doesn't work if the endpoints are exposed from the parent boilerplate project which is being added as a dependency.

In my application.yml, I have added:

management:
    endpoints:
        web:
            base-path: /
            exposure:
                include: '*'

There aren't many resources available and those which are, aren't helping.

1
Please add the project structure, and relevant pom also.Thomas

1 Answers

3
votes

Found the answer.

Instead of creating the bean with @Component, have a configuration file to create all the beans of your endpoints. For example, the configuration file might look like this:

@ManagementContextConfiguration
public class HealthConfiguration {

@Bean
public ActiveUsersEndpoint activeUsersEndpoint() {
    return new ActiveUsersEndpoint();
}
// Other end points if needed...
}

The important thing was to have spring.factories file in resources. The file will point to the configuration file where you created the beans of all your endpoints: org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=com.foo.bar.HealthConfiguration