Spring Boot Actuator Custom Endpoints explained

This article will teach you how to create Custom Spring Boot Actuator endpoints and why this can be beneficial for your Spring Boot applications. We will provide step-by-step details on how to deploy and test your custom Actuator Endpoints.

Introduction to custom Actuator Endpoints

Firstly, if you are new to Spring Boot Actuator, we recommend checking this article to get started: Spring Boot 3 Actuator Essentials

Creating custom Spring Boot Actuator endpoints can be very beneficial for several reason:

  • Custom Monitoring: You might have specific metrics or monitoring needs that are not covered by the default Actuator endpoints. Custom endpoints allow you to expose these metrics, making it easier to monitor the health and performance of your application.
  • Operational Control: Custom endpoints can provide operational control over your application. For instance, you can create endpoints to trigger certain actions like clearing caches, restarting services, or updating configurations without redeploying the application.
  • Integration with external systems: Custom endpoints can facilitate integration with external monitoring and management systems. You can expose endpoints that provide data in a format that these systems can consume, making it easier to integrate your application into a larger ecosystem.

Let’s see in practice how to configure a Custom Spring Boot Actuator Endpoint

Create the Custom Endpoint Class

Create a new class named HelloWorldEndpoint in your project. This class will define the custom Actuator endpoint.

import org.springframework.boot.actuate.endpoint.annotation.DeleteOperation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;

@Endpoint(id = "helloworld")
public class HelloWorldEndpoint {

    @ReadOperation
    public String helloName(String name) {
        return "Hello " + name;
    }

    // Path variable
    @ReadOperation
    public String helloNameSelector(@Selector String name) {
        return "Hello " + name;
    }

    @WriteOperation
    public String helloNameBody(String name) {
        return "Hello " + name;
    }

    @DeleteOperation
    public String goodbyeNameParam(String name) {
        return "Goodbye " + name;
    }

    @DeleteOperation
    public String goodbyeNameSelector(@Selector String name) {
        return "Goodbye " + name;
    }

}

As you can see from the above code, every operation reflects the HTTP command you can use to trigger the operation. For example, the @ReadOperation is accessible through an HTTP GET. Also, the operations which have a @Selector can pass arguments as Path Variables. If you don’t use the @Selector annotation, you will use Query parameters.

Register the Endpoint

Next, register the Custom Endpoint in a Configuration Class:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CustomActuatorConfiguration {

    @Bean
    public HelloWorldEndpoint helloWorldEndpoint() {
        return new HelloWorldEndpoint();
    }
}

Finally, make sure your configuration exposes the endpoint. If you are using the following configuration, all available Actuator endpoints will be exposed:

management.endpoints.web.exposure.include=*

Testing the Custom Endpoint

You can easily test your custom actuator endpoint. For example, let’s test a GET request:

spring boot actuator custom endpoint

Much the same way, you can test the Actuator using a Query parameter:

curl http://localhost:8080/actuator/helloworld?name=john
Hello john

Source code: https://github.com/fmarchioni/masterspringboot/tree/master/actuator

Conclusion

This article was a walk through an example application which publishes a custom Actuator endpoint through all available HTTP verbs.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon