Discover how to make your app tell you exactly what you need without messy code!
Why Custom actuator endpoints in Spring Boot? - Purpose & Use Cases
Imagine you have a Spring Boot app and want to check its health or metrics by writing your own code to expose these details manually.
Manually adding monitoring code everywhere is messy, easy to forget, and hard to keep consistent. It slows development and makes maintenance painful.
Custom actuator endpoints let you add your own monitoring or management features cleanly, integrated with Spring Boot's built-in system.
public String getHealth() { return "OK"; } // scattered checks@Endpoint(id = "custom") public class CustomEndpoint { @ReadOperation public String health() { return "OK"; } }
You can easily add tailored management info accessible via HTTP or JMX, improving app observability without clutter.
A custom endpoint that shows how many users are currently logged in or the status of a special feature toggle.
Manual monitoring code is hard to maintain and inconsistent.
Custom actuator endpoints integrate your checks cleanly into Spring Boot.
This improves app management and helps spot issues faster.
Practice
Solution
Step 1: Understand the purpose of custom actuator endpoints
Custom actuator endpoints allow you to add your own monitoring or info checks in Spring Boot.Step 2: Identify the correct annotation for defining custom endpoints
The@Endpointannotation is specifically designed to mark a class as a custom actuator endpoint.Final Answer:
@Endpoint -> Option AQuick Check:
Custom actuator endpoint = @Endpoint [OK]
- Confusing @Endpoint with @Controller
- Using @Service instead of @Endpoint
- Using @RestController which is for REST APIs
Solution
Step 1: Recall method annotations for custom actuator endpoints
Custom actuator endpoints use specific annotations to define operations: read or write.Step 2: Match read-only operation annotation
The@ReadOperationannotation marks a method as a read-only operation accessible via HTTP GET.Final Answer:
@ReadOperation -> Option BQuick Check:
Read-only method = @ReadOperation [OK]
- Using @WriteOperation for read methods
- Confusing @GetMapping with actuator annotations
- Using @PostMapping which is for REST controllers
@Endpoint(id = "status")
public class StatusEndpoint {
@ReadOperation
public String getStatus() {
return "UP";
}
}Solution
Step 1: Analyze the custom endpoint class
The class is annotated with@Endpoint(id = "status")and has a method with@ReadOperationreturning "UP".Step 2: Understand the HTTP response from the read operation
Accessing the actuator endpoint with id "status" triggers thegetStatus()method, returning the string "UP" as the response body.Final Answer:
"UP" -> Option CQuick Check:
Read operation returns "UP" [OK]
- Expecting 404 if endpoint is defined correctly
- Confusing return value with HTTP status code
- Assuming method returns JSON object instead of string
@Endpoint(id = "metrics")
public class MetricsEndpoint {
@ReadOperation
public String metrics() {
return 12345;
}
}Solution
Step 1: Check method return type consistency
The methodmetrics()returns an integer literal 12345 but declares return type String, causing a type mismatch.Step 2: Verify annotations and method naming
The@ReadOperationannotation is present, and@Endpointis correctly on the class. Method name can be arbitrary.Final Answer:
The method declares String return type but returns an int -> Option DQuick Check:
Return type mismatch causes error [OK]
- Thinking @Endpoint goes on methods
- Assuming method name must follow getter pattern
- Ignoring type mismatch errors
@Endpoint(id = "config")
public class ConfigEndpoint {
@WriteOperation
public String updateConfig(String key, String value) {
// update logic
return "Updated";
}
}
B) @Endpoint(id = "config")
public class ConfigEndpoint {
@ReadOperation
public void updateConfig(String key, String value) {
// update logic
}
}
C) @Endpoint(id = "config")
public class ConfigEndpoint {
@WriteOperation
public void updateConfig() {
// update logic
}
}
D) @Endpoint(id = "config")
public class ConfigEndpoint {
@WriteOperation
public String updateConfig() {
// update logic
return "Updated";
}
}Solution
Step 1: Understand @WriteOperation usage
@WriteOperation marks methods that handle write operations, typically POST, and can accept parameters for input.Step 2: Analyze method signatures and annotations
The correct combination uses@WriteOperationwithString keyandString valueparameters and returns aStringresponse, which is valid for POST updates. Using@ReadOperationis wrong for update operations. The other snippets lack parameters needed to receivekeyandvalue.Final Answer:
Option A is correct because @WriteOperation supports parameters and returns a response -> Option AQuick Check:
@WriteOperation with parameters for POST updates [OK]
- Using @ReadOperation for write/update methods
- Omitting parameters needed for update
- Assuming @WriteOperation methods cannot return values
Ready to prove it?
Pass the test and this pattern moves unknown
