CodexBloom - Programming Q&A Platform

Spring Boot 2.6: how to to configure custom health indicators for Actuator endpoints

๐Ÿ‘€ Views: 96 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-01
spring-boot spring-actuator health-indicator Java

I'm stuck on something that should probably be simple. Can someone help me understand I'm trying to implement a custom health indicator in my Spring Boot 2.6 application, but I'm running into issues getting it registered correctly with the Actuator endpoints. I created a class that implements `HealthIndicator`, but when I access the `/actuator/health` endpoint, my custom health checks are not showing up in the response. Hereโ€™s the code Iโ€™ve written: ```java import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class CustomHealthIndicator implements HealthIndicator { @Override public Health health() { // Perform some custom health check logic boolean isHealthy = checkCustomLogic(); if (isHealthy) { return Health.up().withDetail("Custom Health", "Service is healthy").build(); } return Health.down().withDetail("Custom Health", "Service is down").build(); } private boolean checkCustomLogic() { // Your logic here return true; // Assume it's healthy for now } } ``` I have also ensured that the `spring-boot-starter-actuator` dependency is included in my `pom.xml`: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` When I run the application and hit the `/actuator/health` endpoint, I just see the default health indicators (like `diskSpace`, `db`, etc.) but no sign of my `CustomHealthIndicator`. I've tried to enable all health indicators by adding `management.health.*.enabled=true` in my `application.properties`, but it still doesn't work. ```properties management.endpoint.health.show-details=always management.health.custom.enabled=true ``` Iโ€™ve also tried adding `@ConditionalOnBean` annotations but that didn't seem to help either. Any guidance on what I'm missing? Could it be an scenario with how Iโ€™m configuring the Actuator, or is there something particular with Spring Boot 2.6 I should be aware of? Any ideas what could be causing this? What am I doing wrong?