Prerequisites for Spring Boot Actuator
To use the Spring Boot Actuator, you need to have a few dependencies in place. First, you need to have Spring Boot installed and configured in your project. You also need to have the Spring Boot Actuator dependency included in your project’s pom.xml file if you’re using Maven, or your build.gradle file if you’re using Gradle.
The Spring Boot Actuator provides production-ready features to help you monitor and manage your application. For more information on Spring Boot and its features, you can visit our Spring Boot tutorial.
To include the Spring Boot Actuator in your project, you can add the following dependency to your pom.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Alternatively, if you’re using Gradle, you can add the following dependency to your build.gradle file:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
Here’s an example of a simple Spring Boot application that uses the Spring Boot Actuator:
package com.example.actuator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// We're creating a simple Spring Boot application
@SpringBootApplication
public class ActuatorApplication {
// The main method is the entry point of the application
public static void main(String[] args) {
// We're starting the Spring Boot application
SpringApplication.run(ActuatorApplication.class, args);
}
}
When you run this application, you can access the Spring Boot Actuator endpoints by visiting http://localhost:8080/actuator in your web browser. The expected output will be a list of available endpoints:
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
}
}
}
For further information on configuring and using the Spring Boot Actuator, you can visit our Spring Boot Actuator configuration page.
Understanding Spring Boot Actuator Concepts
Spring Boot Actuator provides a set of endpoints that can be used to monitor and manage a Spring Boot application. These endpoints are used to access various metrics, logs, and other application details. The spring-boot-starter-actuator dependency is used to enable the actuator endpoints in a Spring Boot application. By default, most of the actuator endpoints are secured and not accessible.
Table of Contents
- Prerequisites for Spring Boot Actuator
- Understanding Spring Boot Actuator Concepts
- Step-by-Step Guide to Configuring Spring Boot Actuator
- Full Example of Spring Boot Actuator in Action
- Common Mistakes to Avoid When Using Spring Boot Actuator
- Mistake 1: Incorrect Actuator Endpoint Configuration
- Mistake 2: Insufficient Security Configuration
- Production-Ready Tips for Spring Boot Actuator
- Testing and Validating Spring Boot Actuator Endpoints
- Key Takeaways and Conclusion
- Troubleshooting Common Spring Boot Actuator Issues
The health endpoint is used to check the health of the application, and it provides information about the disk space, database connection, and other application dependencies. The HealthIndicator interface is used to create custom health indicators. For more information on configuring Spring Boot applications, you can refer to our previous article.
The info endpoint is used to display information about the application, such as the build version, git commit hash, and other details. The InfoContributor interface is used to create custom info contributors. The metrics endpoint is used to access various application metrics, such as the number of HTTP requests, memory usage, and other details.
The loggers endpoint is used to configure and view the logging levels of the application. The Logger interface is used to create custom loggers. The actuator endpoints can be accessed using the HTTP protocol or the JMX protocol. For more information on monitoring Spring Boot applications, you can refer to our previous article.
Step-by-Step Guide to Configuring Spring Boot Actuator
To enable **Spring Boot Actuator**, you need to add the spring-boot-starter-actuator dependency to your project. This dependency provides production-ready features to help you monitor and manage your application. You can find more information about the available features in our Spring Boot Actuator Features article.
The first step in configuring **Spring Boot Actuator** is to add the required dependency to your `pom.xml` file if you are using Maven. You can also learn more about Spring Boot project structure to understand where to place your configuration files.
To customize the **actuator** endpoints, you can use the application.properties or application.yml file. For example, you can enable or disable specific endpoints using the management.endpoints.web.exposure.include property.
management.endpoints.web.exposure.include=*
Here is a complete example of a MyActuatorConfig class that demonstrates how to customize the **actuator** endpoints:
package com.example.demo;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.context.ShutdownEndpoint;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class MyActuatorConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests()
.requestMatchers(EndpointRequest.to("health", "info")).permitAll()
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
.and()
.httpBasic();
}
}
When you run the application with the above configuration, you can access the **actuator** endpoints using a tool like `curl`. The expected output for the `health` endpoint would be:
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 999965696,
"free": 739731072,
"threshold": 10485760
}
},
"ping": {
"status": "UP"
}
}
}
For further reading on **Spring Boot security**, you can visit our Spring Boot Security article.
Full Example of Spring Boot Actuator in Action
The **Spring Boot Actuator** provides production-ready features to help you monitor and manage your application. To demonstrate its usage, we will create a simple web application that exposes **health** and **info** endpoints. For a deeper understanding of the **Spring Boot** framework, visit our Spring Boot tutorial to learn more about its core features.
To get started, we need to add the **Spring Boot Actuator** dependency to our `pom.xml` file. This will enable the **actuator** endpoints in our application. We will then create a configuration class to customize the **actuator** settings.
The ActuatorConfig class will be used to expose the **actuator** endpoints.
package com.example.demo;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class ActuatorConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Allow access to actuator endpoints without authentication
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests().anyRequest().permitAll();
}
}
We will also create a custom HealthIndicator to provide additional health information. For further reading on **health indicators**, visit our Spring Boot health indicators tutorial.
package com.example.demo;
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() {
// Return a health status based on custom logic
return Health.up().build();
}
}
When we run the application and access the **/actuator/health** endpoint, we should see a response similar to the following:
{
"status": "UP",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 999652569984,
"free": 739335449984,
"threshold": 10485760
}
},
"ping": {
"status": "UP"
},
"custom": {
"status": "UP"
}
}
}
This demonstrates how the **Spring Boot Actuator** can be used to monitor and manage a web application. For more information on **Spring Boot** and its features, visit our Spring Boot features tutorial.
Common Mistakes to Avoid When Using Spring Boot Actuator
When using Spring Boot Actuator, developers often encounter common pitfalls that can hinder the monitoring and management of their applications. One of the primary reasons for these mistakes is the lack of understanding of the actuator endpoints and their configuration. To learn more about configuring actuator endpoints, visit our Spring Boot Actuator Endpoints tutorial.
Mistake 1: Incorrect Actuator Endpoint Configuration
A common mistake is incorrectly configuring the actuator endpoints. For example, the following code snippet shows an incorrect configuration:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// WRONG
@SpringBootApplication(exclude = { "org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration" })
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
This configuration will result in an error message indicating that the EndpointAutoConfiguration class is not found. The correct configuration should include the actuator endpoints:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Correct configuration
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The expected output will be the successful startup of the Spring Boot application with the actuator endpoints enabled:
2023-12-01 12:00:00.000 INFO 12345 --- [ main] o.s.b.a.e.web.EndpointAutoConfiguration
Mistake 2: Insufficient Security Configuration
Another common mistake is insufficient security configuration for the actuator endpoints. For example, the following code snippet shows an incorrect security configuration:
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
// WRONG
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll();
}
}
This configuration will allow unauthorized access to the actuator endpoints. To learn more about securing actuator endpoints, visit our Spring Boot Actuator Security tutorial. The correct configuration should include proper authentication and authorization:
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
// Correct configuration
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated();
}
}
The expected output will be the successful authentication and authorization of the actuator endpoints. For further reading on Spring Boot Actuator, visit our Spring Boot Actuator Tutorial.
Production-Ready Tips for Spring Boot Actuator
When deploying Spring Boot Actuator in a production environment, it is essential to follow best practices to ensure the security and reliability of your application. The actuator provides a range of endpoints for monitoring and managing your application, including the health endpoint, which provides information about the health of your application. To restrict access to these endpoints, you can use Spring Security to secure the actuator endpoints. For more information on securing your application, see our guide on configuring Spring Security.
Production tip: Use the
management.endpoints.web.exposure.includeproperty to control which actuator endpoints are exposed over the web.
By default, all actuator endpoints are exposed over the web, which can pose a security risk. To mitigate this risk, you can use the management.endpoints.web.exposure.include property to specify which endpoints should be exposed. For example, you can expose only the health and info endpoints.
Production tip: Use a logging framework such as Logback or Log4j to configure logging for your application, and use the
logging.levelproperty to control the log level for the actuator endpoints.
Logging is an essential aspect of monitoring and debugging your application. By using a logging framework and configuring the log level for the actuator endpoints, you can gain valuable insights into the behavior of your application. For more information on logging in Spring Boot, see our guide on configuring logging in Spring Boot.
Production tip: Use the
management.metrics.exportproperty to export metrics from your application to a metrics backend such as Prometheus or Graphite.
Exporting metrics from your application can provide valuable insights into its performance and behavior. By using the management.metrics.export property, you can export metrics to a metrics backend such as Prometheus or Graphite, where they can be visualized and analyzed. For more information on metrics in Spring Boot, see our guide on configuring metrics in Spring Boot.
Testing and Validating Spring Boot Actuator Endpoints
To ensure the **Spring Boot** actuator endpoints are functioning correctly, it is crucial to test and validate them. This can be achieved by using **JUnit** tests to verify the endpoints return the expected responses. The TestRestTemplate class can be used to send HTTP requests to the actuator endpoints.
When testing the actuator endpoints, it is essential to consider the **security** configuration of the application. If the actuator endpoints are secured, the tests must be configured to authenticate with the application. For more information on securing **Spring Boot** applications, refer to our article on Configuring Spring Boot Security.
The following example demonstrates how to test the **health** actuator endpoint:
package com.example.actuator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ActuatorTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHealthEndpoint() throws Exception {
// Send a GET request to the health endpoint
mockMvc.perform(MockMvcRequestBuilders.get("/actuator/health"))
// Verify the response status is 200 (OK)
.andExpect(MockMvcResultMatchers.status().isOk())
// Verify the response contains the expected health status
.andExpect(MockMvcResultMatchers.jsonPath("$.status").value(Health.Status.UP.toString()));
}
}
The expected output of the test will be:
OK (200) with JSON response: {"status":"UP"}
By writing comprehensive tests for the actuator endpoints, developers can ensure their **Spring Boot** application is properly monitored and maintained. For further reading on **Spring Boot** actuator configuration, refer to our article on Configuring Spring Boot Actuator.
Key Takeaways and Conclusion
The Spring Boot **actuator** provides a powerful tool for monitoring and managing applications. By utilizing the spring-boot-starter-actuator dependency, developers can gain insight into their application’s internal state, including **metrics**, **health**, and **info** endpoints. The Actuator also supports **HTTP** and **JMX** endpoints, allowing for flexible monitoring and management.
The **actuator** endpoints can be accessed via HTTP requests, and the management.endpoints.web.exposure.include property can be used to configure which endpoints are exposed. For example, to expose the **health** endpoint, the following configuration can be used: management.endpoints.web.exposure.include=health. This allows developers to monitor the health of their application and take corrective action when necessary.
To further customize the **actuator**, developers can use the management.endpoint properties to configure individual endpoints. For more information on customizing the **actuator**, see our article on Customizing the Spring Boot Actuator. By leveraging the **actuator** and its various endpoints, developers can build more robust and maintainable applications.
In conclusion, the Spring Boot **actuator** provides a valuable tool for monitoring and managing applications. By understanding the various **actuator** endpoints and how to configure them, developers can gain greater insight into their application’s internal state and improve overall application health. The actuator is a powerful tool that can help developers build better applications, and its use is highly recommended for any Spring Boot project.
Troubleshooting Common Spring Boot Actuator Issues
When working with Spring Boot Actuator, you may encounter issues that prevent you from effectively monitoring and managing your application. One common issue is the java.lang.NoClassDefFoundError exception, which occurs when the Actuator dependencies are not properly configured. To resolve this, ensure that you have included the necessary dependencies in your pom.xml file, such as spring-boot-starter-actuator. You can refer to our Spring Boot Actuator configuration guide for more information on setting up the Actuator.
Another issue you may face is the inability to access the Actuator endpoints, such as /health or /info. This can be due to the endpoints being disabled or not properly exposed. To resolve this, check your application.properties file and ensure that the Actuator endpoints are enabled and exposed. You can do this by setting the management.endpoints.web.exposure.include property to include the desired endpoints.
When using Spring Boot Actuator with security enabled, you may encounter issues with authentication and authorization. To resolve these issues, ensure that you have properly configured the security settings for the Actuator endpoints. You can do this by setting the management.endpoints.web.base-path property and configuring the security settings for the base path. For more information on securing your Spring Boot application, you can refer to our Spring Boot security configuration guide.
In addition to these common issues, you may also encounter problems with the Actuator metrics and logging. To resolve these issues, ensure that you have properly configured the metrics and logging settings for your application. You can do this by setting the management.metrics.export property and configuring the logging settings for your application. By following these troubleshooting steps and referring to the relevant guides, you can effectively resolve common Spring Boot Actuator issues and ensure that your application is properly monitored and managed.
spring-boot-examples — Clone, Star & Contribute

Leave a Reply