Quick revision
The short answer
We create a bean annotated with @Endpoint(id = "featureflags"), add a @ReadOperation method that returns the map of flags, and a @WriteOperation method whose @Selector String name parameter comes from the path and whose boolean parameter comes from the JSON body. Then we expose it with management.endpoints.web.exposure.include and protect the write operation with an operator role. If the endpoint should only exist over HTTP and not JMX, we use @WebEndpoint instead of @Endpoint.
What to remember
In this answer, we built a custom actuator endpoint with @Endpoint(id = "featureflags"), a @ReadOperation that lists the flags, a selector-based read for one flag and a @WriteOperation that takes the name from the path and the value from the JSON body. We exposed it through management.endpoints.web.exposure.include, protected its writes with an operator role, saw when @WebEndpoint is the better choice, and noted that real flags across many pods need a shared store behind the endpoint.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We create a bean annotated with @Endpoint(id = "featureflags"), add a @ReadOperation method that returns the map of flags, and a @WriteOperation method whose @Selector String name parameter comes from the path and whose boolean parameter comes from the JSON body. Then we expose it with management.endpoints.web.exposure.include and protect the write operation with an operator role. If the endpoint should only exist over HTTP and not JMX, we use @WebEndpoint instead of @Endpoint.
Why this question is asked
Actuator is not only a set of built-in endpoints; it is a small framework for operational endpoints. Writing our own gives us the same base path, the same exposure rules, the same security matcher and JMX support for free, which is why teams use it for things like feature flags or cache controls. Interviewers ask this coding question to see whether we know the annotations and how Actuator maps method parameters to HTTP. In this answer, we will recap the endpoint model, write the endpoint, call it over HTTP, expose and secure it, and look at the design limits of a per-pod flag store.
1A Quick Recap of the Actuator Endpoint Model
An actuator endpoint is a bean with an id and a set of operations. Actuator reads the annotations at startup and creates the HTTP mappings, and optionally JMX operations, from them. We do not write any @RestController code.
There are three operation types, and they map to HTTP methods. A @ReadOperation becomes a GET, a @WriteOperation becomes a POST, and a @DeleteOperation becomes a DELETE. A parameter marked @Selector becomes part of the path, so a selector called name turns /actuator/featureflags into /actuator/featureflags/{name}. For a write operation, the other parameters are read from the JSON request body by name. Because Actuator binds parameters by name, the code must be compiled with the -parameters flag, which spring-boot-starter-parent and the Boot Gradle plugin configure for us.
| Annotation | HTTP | Parameters come from |
|---|---|---|
@ReadOperation | GET | @Selector in path, others as query parameters |
@WriteOperation | POST | @Selector in path, others from the JSON body |
@DeleteOperation | DELETE | @Selector in path |
@Endpoint | HTTP and JMX | Technology-neutral endpoint |
@WebEndpoint | HTTP only | Same operations, no JMX |
2Writing the Endpoint
Here is the endpoint. The flags live in a ConcurrentHashMap, because requests can arrive on several threads at once.
Notice the two read operations. The one without a selector answers GET /actuator/featureflags; the one with a selector answers GET /actuator/featureflags/{name}. When a read operation returns null, Actuator responds with 404, which is exactly right for an unknown flag. A write operation that returns nothing responds with 204.
We reject unknown flag names on purpose. Without that check, a typo in an operator's request would quietly create a new flag that no code reads, and the operator would believe the change had worked. InvalidEndpointRequestException is Actuator's own exception for a bad request, so the caller gets a 400 with the reason instead of a 500 that looks like a server fault.
3Calling It Over HTTP
Once exposed, the endpoint behaves like any other actuator endpoint.
6 messages in order. Play them, or step through them one at a time.
The body field must be called enabled, the same as the method parameter. Because enabled is a primitive boolean, it is mandatory, so a request without it is rejected with 400. If a parameter should be optional, we use a wrapper type and mark it with @Nullable (check your version: newer releases may prefer their own optional-parameter annotation).
4Exposing and Securing It
A custom endpoint follows the same exposure rules as built-in ones, so it is not reachable over HTTP until we add its id. And because it changes behaviour, the write operation must be protected.
Here support staff can read flags but only operators can change them. The GET rule uses a path because EndpointRequest does not distinguish HTTP methods; if the base path changes, that rule must change too. Every change should also be logged with the user's name, for example by logging inside setFlag with the current authentication, so there is an audit trail of who switched what.
5@Endpoint or @WebEndpoint?
@Endpoint is technology-neutral: Actuator offers it over HTTP and, if JMX is enabled with spring.jmx.enabled=true, as an MBean. @WebEndpoint creates the same operations for HTTP only. For a feature flag endpoint, I would use @WebEndpoint when the team does not use JMX, because a second, separately secured channel that can change state is one more thing to protect. Spring Boot also offers @JmxEndpoint for the opposite case.
6A Design Limit Worth Mentioning
This endpoint changes a map inside one JVM. With 10 pods, a POST reaches one of them, so one pod has the flag on and nine have it off, and a restart resets it. That is fine for a single-instance tool or for local testing, but for real feature flags across a fleet we should keep the state in a shared store, such as a database table or a feature flag service, and let the endpoint read and write that store. The endpoint then becomes a thin operational window onto shared state, which is what operators expect.
7Follow-up questions
How do I return a custom status code from an operation?
Return a WebEndpointResponse, which wraps a body and a status code, for example new WebEndpointResponse<>(body, 409). It ties the operation to the web technology, so it fits naturally with @WebEndpoint.
Can a read operation take extra parameters?
Yes. Non-selector parameters of a read operation come from the query string, so a method flags(@Nullable String prefix) would answer GET /actuator/featureflags?prefix=new.
How would you test the endpoint?
Unit-test the class directly by calling its methods, then add a @SpringBootTest with MockMvc or WebTestClient that calls the HTTP paths, checking 200 for reads, 204 for a write, 404 for an unknown flag on read, and 401 or 403 for callers without the operator role.
8Conclusion
In this answer, we built a custom actuator endpoint with @Endpoint(id = "featureflags"), a @ReadOperation that lists the flags, a selector-based read for one flag and a @WriteOperation that takes the name from the path and the value from the JSON body. We exposed it through management.endpoints.web.exposure.include, protected its writes with an operator role, saw when @WebEndpoint is the better choice, and noted that real flags across many pods need a shared store behind the endpoint.
Open with
I would write a bean annotated with @Endpoint, id featureflags, which Actuator turns into /actuator/featureflags. It holds the flags in a ConcurrentHashMap. A @ReadOperation method returns a copy of the whole map, so a GET lists every flag.
Then, if they want more
I would add a second read operation with a @Selector String name parameter for one flag, which returns 404 when the result is null.
For changes, a @WriteOperation method takes a @Selector String name and a boolean enabled; Actuator maps it to POST /actuator/featureflags/{name}, and the enabled value is read from the JSON body by parameter name, which works because Boot compiles with the -parameters flag. I reject unknown names so a typo does not silently create a flag.
Then I expose it with management.endpoints.web.exposure.include, since custom endpoints are not exposed by default, and in the actuator security chain I require an operator role for the write and log who changed what. If nobody uses JMX, I would use @WebEndpoint so it is HTTP only.
And I would point out that an in-memory map is per pod, so for a real fleet the endpoint should read and write a shared store.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference, Actuator
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a custom actuator endpoint /actuator/featureflags that lists flags and allows...": https://scrivio-interview-library.netlify.app/spring-boot/actuator/82ea59e7df/