Backend Development 4 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Build Custom Spring Boot Actuator Endpoints with @Endpoint, @ServletEndpoint, and @RestControllerEndpoint

Environment: Spring Boot 2.4.12

1. Environment preparation

Add the Actuator starter dependency:

<code><dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</code>

Actuator configuration (example):

<code>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
          - logfile
</code>

If 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:

<code>@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;
    }
}
</code>

Access: http://localhost:8888/actuator/aqs

ServletEndpointsSupplier

Define a regular servlet:

<code>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);
    }
}
</code>

Register it as an endpoint:

<code>@Component
@ServletEndpoint(id = "akf")
public class AKFEndpoint implements Supplier<EndpointServlet> {
    @Override
    public EndpointServlet get() {
        return new EndpointServlet(AKFServlet.class);
    }
}
</code>

Access: http://localhost:8888/actuator/akf

ControllerEndpointsSupplier

Use @RestControllerEndpoint or @ControllerEndpoint:

<code>@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";
    }
}
</code>

Access URLs:

http://localhost:8888/actuator/cas/cas

http://localhost:8888/actuator/cas/mpp

Official documentation recommends preferring @Endpoint/@WebEndpoint over tightly coupled servlet or controller approaches.

The three methods above illustrate how to implement custom Actuator endpoints.

backendJavaSpringBootSpring FrameworkActuatorcustom-endpoint
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.