How to get Spring Actuator Health Status through Java Code?

So How Do I consume the Actuator response within my applciation too?

Typically I have seen developers invoking the health web endpoint and parse the output. Which is okay but requires a lot of boiler plate code and it’s error prone.

  1. Aggregated Health Status
import org.springframework.stereotype.Component;
import org.springframework.boot.actuate.health.Status;
@Component
public class MyHealthStatusProvider {
private final HealthEndpoint healthEndpoint; public MyHealthStatusProvider(HealthEndpoint healthEndpoint) {
this.healthEndpoint = healthEndpoint;
}
public Status getHealthStatus() {
return this.healthEndpoint.health().getStatus();
}
}
import org.springframework.boot.actuate.health.HealthIndicator;@Component
public class MyComponentHealthStatusProvider {
private final List<HealthIndicator> healthIndicators; public MyHealthStatusProvider(List<HealthIndicator> healthIndicators) {
this.healthIndicators = healthIndicators;
}
public List<HealthIndicator> getHealthStatus() {
return this.healthIndicators;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store