0
0
Spring Bootframework~5 mins

Custom actuator endpoints in Spring Boot

Choose your learning style9 modes available
Introduction

Custom actuator endpoints let you add your own health or info checks to a Spring Boot app. This helps you see app details or status easily.

You want to show extra app info like version or config values.
You need to check a custom service or resource health.
You want to expose metrics specific to your app.
You want to add a simple status check for external tools.
You want to extend Spring Boot's monitoring with your own data.
Syntax
Spring Boot
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "custom")
public class CustomEndpoint {

    @ReadOperation
    public String customInfo() {
        return "Hello from custom endpoint!";
    }
}

The @Endpoint annotation defines a new actuator endpoint with a unique id.

The @ReadOperation marks a method that handles GET requests to this endpoint.

Examples
A simple endpoint named 'status' that returns a running message.
Spring Boot
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "status")
public class StatusEndpoint {

    @ReadOperation
    public String status() {
        return "App is running";
    }
}
This endpoint named 'counter' shows a number and lets you increase it via POST.
Spring Boot
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "counter")
public class CounterEndpoint {
    private int count = 0;

    @ReadOperation
    public int getCount() {
        return count;
    }

    @WriteOperation
    public void increment() {
        count++;
    }
}
Sample Program

This Spring Boot app adds a custom actuator endpoint at '/actuator/greeting' that returns a greeting message.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Component
@Endpoint(id = "greeting")
class GreetingEndpoint {

    @ReadOperation
    public String greet() {
        return "Hello from custom actuator endpoint!";
    }
}
OutputSuccess
Important Notes

Make sure to enable your custom endpoint in application.properties if actuator endpoints are restricted.

Custom endpoints appear under the main actuator URL, like /actuator/{id}.

Use @WriteOperation for POST/PUT actions on your endpoint.

Summary

Custom actuator endpoints let you add your own monitoring or info checks.

Use @Endpoint with @ReadOperation or @WriteOperation to define them.

They help you see app status or data easily through HTTP requests.