'Direct access to Spring Boot Actuator Health beans/data?
From a Sprig Boot application is it possible access the actuator/health data directly without making a rest call and parsing the results?
Ideally I could autowire a bean and then be able to fetch the object representation of the health data with a method call.
For example if my health endpoint is showing something like this:
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "PostgreSQL",
"result": 1,
"validationQuery": "SELECT 1"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 499963174912,
"free": 389081399296,
"threshold": 10485760
}
},
"ping": {
"status": "UP"
},
"redis": {
"status": "UP",
"details": {
"version": "3.2.12"
}
}
}
}
Then which components could I autowire to find out each of those bits of information?
Solution 1:[1]
Sure you can inject the according Endpoint. For example if you are interested in the HealthEndpoint you could do:
@RestController
public class ActuatorController {
private final HealthEndpoint healthEndpoint;
public ActuatorController(HealthEndpoint healthEndpoint) {
this.healthEndpoint = healthEndpoint;
}
@GetMapping("health")
public String health() {
return healthEndpoint.health().getStatus().getCode();
}
}
Solution 2:[2]
Also, the info and health points are enabled in actuator by default and you don't need to expose them manually. The moment you add the dependency to your pom.xml it will be enabled and you can access the url endpoints without exposing them
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simon Martinelli |
| Solution 2 | Amit Mishra |
