How to Add Spring Boot Actuator /health Endpoint to Legacy Spring Apps
This guide explains how to integrate Spring Boot Actuator into a traditional Spring application by manually adding required Maven dependencies, importing core Actuator configuration classes, and defining beans that expose the /health and /metrics monitoring endpoints.
Add Required Dependencies
Legacy Spring projects cannot use Spring Boot starter POMs directly, so you must copy the necessary spring-boot-starter-actuator dependency into your pom.xml manually:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Manual Configuration
Because a traditional Spring application lacks Spring Boot’s auto‑configuration, you need to create a configuration class that imports the Actuator’s core auto‑configuration classes and registers the required beans.
@Import({
EndpointAutoConfiguration.class, // basic Actuator infrastructure
HealthIndicatorAutoConfiguration.class // /health endpoint support
})
@Configuration
public class ActuatorConfig {
@Bean
public EndpointHandlerMapping endpointHandlerMapping() {
// loads all Actuator endpoints
return new EndpointHandlerMapping();
}
@Bean
public HealthMvcEndpoint healthMvcEndpoint() {
// implementation of the /health endpoint
return new HealthMvcEndpoint();
}
}After adding this configuration, start the application and you will see the /health endpoint printed in the console. Access it via a browser or HTTP client to obtain the current health status of the instance.
Create Additional Endpoints (e.g., /metrics)
If you also want to expose other Actuator endpoints such as /metrics, import the PublicMetricsAutoConfiguration class and define a bean that implements the metrics endpoint.
@Import(PublicMetricsAutoConfiguration.class)
@Configuration
public class MetricsConfig {
@Bean
public MetricsMvcEndpoint metricsMvcEndpoint() {
return new MetricsMvcEndpoint();
}
}With these steps completed, the legacy Spring application now provides the same monitoring capabilities as a Spring Boot service, allowing Spring Cloud components (Eureka, Consul, etc.) to perform health checks and collect metrics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
