Build Custom Spring Boot Actuator Endpoints with @Endpoint, @ServletEndpoint, and @RestControllerEndpoint
This guide shows how to set up Spring Boot 2.4.12, add the Actuator starter, configure custom ports, and create custom Actuator endpoints using three approaches—@Endpoint, @ServletEndpoint, and @RestControllerEndpoint—complete with code samples and access URLs.
Environment: Spring Boot 2.4.12
1. Environment preparation
Add the Actuator starter dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Actuator configuration (example):
management:
endpoints:
jmx:
exposure:
exclude:
- beans
- env
---
management:
server:
port: 8888
endpoints:
web:
path-mapping:
beans: 'mybeans' # modify default path
exposure:
include:
- akf # custom
- cas # custom
- aqs # custom
- beans
- mybeans
- health
- info
- env
- caches
- loggers
- conditions
- configprops
- httptrace
- integrationgraph
- metrics
- mappings
- scheduledtasks
- threaddump
- shutdown
- heapdump
- logfileIf no separate port is configured, Actuator uses the main server port.
2. Define Endpoints
Three ways to register an endpoint:
WebEndpointsSupplier (recommended)
Use @Endpoint on a component class:
@Component
@Endpoint(id = "aqs")
public class AQSEndpoint {
@ReadOperation
public Map<String, Object> info() {
Map<String, Object> res = new HashMap<>();
res.put("AQS", "AQS Endpoint by @Endpoint And @ReadOperation");
return res;
}
}Access:
http://localhost:8888/actuator/aqsServletEndpointsSupplier
Define a regular servlet:
public class AKFServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("AKF Endpoint by @ServletEndpoint");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}Register it as an endpoint:
@Component
@ServletEndpoint(id = "akf")
public class AKFEndpoint implements Supplier<EndpointServlet> {
@Override
public EndpointServlet get() {
return new EndpointServlet(AKFServlet.class);
}
}Access:
http://localhost:8888/actuator/akfControllerEndpointsSupplier
Use @RestControllerEndpoint or @ControllerEndpoint:
@RestController
@RestControllerEndpoint(id = "cas")
public class CASController {
@GetMapping("/cas")
public Object cas() {
return "cas Endpoint by @RestControllerEndpoint";
}
@GetMapping("/mpp")
public Object mpp() {
return "mpp Endpoint by @RestControllerEndpoint";
}
}Access URLs:
http://localhost:8888/actuator/cas/cas http://localhost:8888/actuator/cas/mppOfficial documentation recommends preferring @Endpoint/@WebEndpoint over tightly coupled servlet or controller approaches.
The three methods above illustrate how to implement custom Actuator endpoints.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
