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.
Custom actuator endpoints in 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.
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"; } }
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++; } }
This Spring Boot app adds a custom actuator endpoint at '/actuator/greeting' that returns a greeting message.
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!"; } }
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.
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.
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
