The health endpoint shows if your app is working well. Customizing it helps you add extra checks or change what info it shows.
Health endpoint customization in Spring Boot
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() { // Your custom health check logic boolean serviceUp = checkService(); if (serviceUp) { return Health.up().withDetail("service", "Available").build(); } else { return Health.down().withDetail("service", "Not Available").build(); } } private boolean checkService() { // Check your service here return true; } }
Implement HealthIndicator and override health() to add custom checks.
Use Health.up() or Health.down() to set status and add details with withDetail().
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class DatabaseHealthIndicator implements HealthIndicator { @Override public Health health() { boolean dbUp = checkDatabase(); if (dbUp) { return Health.up().withDetail("database", "Connected").build(); } else { return Health.down().withDetail("database", "Disconnected").build(); } } private boolean checkDatabase() { // Simulate DB check return true; } }
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class CustomMessageHealthIndicator implements HealthIndicator { @Override public Health health() { return Health.up().withDetail("message", "All systems go!").build(); } }
This Spring Boot app has a custom health indicator that adds a "service" detail to the health endpoint. It returns "up" status with a message if the service is running.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @Component class CustomHealthIndicator implements HealthIndicator { @Override public Health health() { boolean serviceRunning = checkService(); if (serviceRunning) { return Health.up().withDetail("service", "Running smoothly").build(); } else { return Health.down().withDetail("service", "Service down").build(); } } private boolean checkService() { // Simulate service check logic return true; } }
Custom health indicators automatically add their info to the main health endpoint at /actuator/health.
You can combine multiple health indicators for a full health report.
Remember to enable actuator endpoints in your application.properties if needed.
Health endpoint shows app status and can be customized to add checks.
Create a class implementing HealthIndicator and override health().
Use Health.up() or Health.down() with details to customize output.
Practice
Solution
Step 1: Understand the health endpoint role
The health endpoint shows the app's status and can be extended to include custom checks.Step 2: Identify customization purpose
Customizing allows adding specific checks to provide detailed status beyond default info.Final Answer:
To add specific checks that show detailed app status -> Option CQuick Check:
Health endpoint customization = detailed app status [OK]
- Thinking it changes app port
- Assuming it disables health checks
- Believing it restarts app automatically
Solution
Step 1: Recall Spring Boot health customization
Custom health checks require implementing a specific interface.Step 2: Identify correct interface
The interface to implement isHealthIndicator, which requires overridinghealth().Final Answer:
HealthIndicator -> Option AQuick Check:
Custom health check interface = HealthIndicator [OK]
- Using HealthCheck instead of HealthIndicator
- Confusing with HealthService or HealthStatus
- Not overriding health() method
public class MyHealth implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("db", "reachable").build();
}
}Solution
Step 1: Analyze health() method return
The method returnsHealth.up()with a detail key "db" set to "reachable".Step 2: Understand output format
The health endpoint shows status "UP" and includes the details map with "db":"reachable".Final Answer:
{"status":"UP","details":{"db":"reachable"}} -> Option AQuick Check:
Health.up() with details = status UP plus details [OK]
- Confusing UP with DOWN status
- Ignoring details in output
- Assuming default status without details
public class CustomHealth implements HealthIndicator {
public Health health() {
return Health.down().withDetails("error", "Database unreachable").build();
}
}Solution
Step 1: Check method signature and annotations
The method correctly namedhealth()matches interface; @Override annotation is optional but recommended.Step 2: Verify method calls on Health builder
The methodwithDetails()does not exist; correct method iswithDetail()for adding one detail.Final Answer:
withDetails() method does not exist, should be withDetail() -> Option BQuick Check:
Use withDetail() to add single detail [OK]
- Missing @Override annotation (not error but best practice)
- Wrong method name instead of health()
- Thinking Health.down() is invalid
Solution
Step 1: Understand combined health logic
The health status should be UP only if both database and cache are reachable; otherwise DOWN.Step 2: Implement conditional return with details
Return Health.up() if both pass; else return Health.down() and include details explaining which failed.Final Answer:
Return Health.up() if both checks pass; else Health.down() with details for each failure -> Option DQuick Check:
Combined checks control status UP or DOWN with details [OK]
- Returning UP regardless of failures
- Ignoring combined status logic
- Splitting checks without combining results
Ready to prove it?
Pass the test and this pattern moves unknown
