java / intermediate
Snippet
Custom Health Indicators with Spring Boot Actuator
Custom health indicators allow you to monitor the status of external dependencies like APIs or databases beyond the default Spring Boot checks. These statuses are exposed via the /actuator/health endpoint.
snippet.java
1
2
3
4
5
6
7
8
9
10
11
12
@Componentpublic class ExternalApiHealthIndicator implements HealthIndicator {@Overridepublic Health health() {boolean isUp = checkService();if (isUp) {return Health.up().withDetail("externalService", "Running").build();}return Health.down().withDetail("externalService", "Unreachable").build();}private boolean checkService() { /* Logic */ return true; }}
spring
Breakdown
1
@Component
Registers the class as a Spring-managed bean.
2
implements HealthIndicator
Mandatory interface for providing health information.
3
Health.up() / Health.down()
Fluent API to build the status and additional metadata.