Sub topic 13
Creating a custom Actuator endpoint
This chapter assumes you can run a Spring Boot application, you know how endpoint exposure works (management.endpoints.web.exposure.include), and you have read the chapters on securing endpoints. Here you will write your own endpoint with read and write operations.
When a custom endpoint makes sense
The built-in endpoints cover health, metrics, and diagnostics. A custom endpoint fits when operators need a small, application-specific control or report. Examples are a feature-flag switch, a cache summary, or the state of a background job. A custom endpoint gets Actuator's exposure rules, security integration, and JSON output for free.
The endpoint class
An endpoint is a Spring bean annotated with @Endpoint. The id becomes the path segment under /actuator.
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "features")
public class FeaturesEndpoint {
private final Map<String, Boolean> features = new ConcurrentHashMap<>();
@ReadOperation
public Map<String, Boolean> features() {
return features;
}
@WriteOperation
public void configure(@Selector String name, boolean enabled) {
features.put(name, enabled);
}
}The annotations map to operations:
| Annotation | HTTP method | JMX operation type |
|---|---|---|
| `@ReadOperation` | GET | Read-only |
| `@WriteOperation` | POST | Modifying |
| `@DeleteOperation` | DELETE | Modifying |
@Selector turns a parameter into a path variable. Here configure is reachable at /actuator/features/{name}. The enabled parameter without @Selector is read from the JSON request body.
Exposing and calling it
An endpoint is not available over HTTP until its id is in the exposure list.
management:
endpoints:
web:
exposure:
include: health,info,featuresThen call it:
curl -X POST http://localhost:8080/actuator/features/dark-mode \
-H "Content-Type: application/json" \
-d '{"enabled": true}'
curl http://localhost:8080/actuator/featuresThe POST returns 204 No Content because the method returns void. The GET returns {"dark-mode":true}.
Common errors
- 404 on the new endpoint. The id is missing from the exposure list. Add
featuresor use*in development only. - Write operation rejected. With the security chain from the earlier chapter, POST needs the
ACTUATORrole and HTTP Basic credentials. If CSRF protection is active for that chain, POST requests are also rejected until you handle it. - Parameters not bound. Parameter names are used for binding, so compile with
-parameters. Spring Boot's Maven and Gradle plugins configure this by default. - Using
@Endpointwhen you only want HTTP.@Endpointis technology-agnostic and also appears in JMX. Use@WebEndpointfor HTTP only.
Testing the logic
The endpoint is a plain class, so you can test it without starting a server.
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class FeaturesEndpointTest {
@Test
void configureStoresFlag() {
FeaturesEndpoint endpoint = new FeaturesEndpoint();
endpoint.configure("dark-mode", true);
assertTrue(endpoint.features().get("dark-mode"));
}
}Exercise
You add a @ReadOperation method to an @Endpoint(id = "features") bean, but GET /actuator/features returns 404. What is the most likely cause, and how do you fix it?
Show answer
The endpoint id is not in management.endpoints.web.exposure.include. Add features to that list (for example health,info,features) and restart the application.
Exercise
Which annotation and parameter style makes /actuator/features/dark-mode pass dark-mode into a method argument?
Show answer
Use @WriteOperation or @ReadOperation with a @Selector parameter, for example configure(@Selector String name, boolean enabled). The selector value is taken from the path segment.