Complete the code to define a simple health check endpoint in Spring Boot.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HealthController { @GetMapping("/health") public String [1]() { return "OK"; } }
The method name can be anything, but health matches the endpoint and is clear.
Complete the Dockerfile line to add a health check that calls the Spring Boot health endpoint.
HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost:8080/[1] || exit 1
The health endpoint is usually /health in Spring Boot apps.
Fix the error in the Dockerfile health check command to correctly check the Spring Boot app health.
HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost:8080/[1] || exit 1
The correct health endpoint is /health. Others will cause errors.
Fill both blanks to create a Spring Boot actuator health check configuration and Dockerfile health check command.
management.endpoint.health.[1]=true HEALTHCHECK --interval=30s CMD curl -f http://localhost:8080/actuator/[2] || exit 1
Enable the health endpoint in Spring Boot actuator with enabled and check /actuator/health in Docker.
Fill all three blanks to define a custom health indicator bean and use it in the health endpoint.
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 [1]() { boolean serverUp = checkServer(); if (serverUp) { return Health.[2]().withDetail("server", "Up").build(); } else { return Health.[3]().withDetail("server", "Down").build(); } } private boolean checkServer() { // Custom logic here return true; } }
The method overriding HealthIndicator is health(). Use Health.up() and Health.down() to indicate status.