How to make the `@Endpoint(id = "health")` working in Spring Boot 2.0?

Update

  • The documentation on the new Spring Actuator Endpoints is not very lucid. It's trying to explain the new endpoint infrastructure with the existing health endpoint as an example.

  • A new endpoint ID has to be unique and shouldn't be same as an existing actuator endpoint. If one tries to the change the ID of the example shown below to health, one will get the following exception:

     java.lang.IllegalStateException: Found two endpoints with the id 'health'
    
  • The above comment about declaring the endpoint classes with @Bean annotation is correct.

  • Customizing the health endpoint hasn't changed in Spring Boot 2.0. You still have to implement HealthIndicator to add custom values.

Custom Actuator Endpoint

Here are the changes needed to create a custom Actuator endpoint in Spring Boot 2.0.

Model

The domain containing your custom information.

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class MyHealth {

    private Map<String, Object> details;

    @JsonAnyGetter
    public Map<String, Object> getDetails() {
        return this.details;
    }
}

My Health Endpoint

Declaring myhealth endpoint,

@Endpoint(id = "myhealth")
public class MyHealthEndpoint {

    @ReadOperation
    public MyHealth health() {
        Map<String, Object> details = new LinkedHashMap<>();
        details.put("MyStatus", "is happy");
        MyHealth health = new MyHealth();
        health.setDetails(details);

        return health;
    }
}

My Health Extension

Extension for myhealth endpoint,

@WebEndpointExtension(endpoint = MyHealthEndpoint.class)
public class MyHealthWebEndpointExtension {

    private final MyHealthEndpoint delegate;

    public MyHealthWebEndpointExtension(MyHealthEndpoint delegate) {
        this.delegate = delegate;
    }

    @ReadOperation
    public WebEndpointResponse<MyHealth> getHealth() {
        MyHealth health = delegate.health();
        return new WebEndpointResponse<>(health, 200);
    }
}

Actuator Configuration

Configuration to expose the two newly created actuator classes as beans,

@Configuration
public class ActuatorConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    public MyHealthEndpoint myHealthEndpoint() {
        return new MyHealthEndpoint();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    @ConditionalOnBean({MyHealthEndpoint.class})
    public MyHealthWebEndpointExtension myHealthWebEndpointExtension(
            MyHealthEndpoint delegate) {
        return new MyHealthWebEndpointExtension(delegate);
    }
}

Application Properties

Changes to application.yml,

endpoints:
  myhealth:
    enabled: true

Once you start your application, you should be able to access the newly actuator endpoint at http://<host>:<port>/application/myhealth.

You should expect a response similar to one shown below,

{
  "MyStatus": "is happy"
}

A complete working example can be found here.