Quick revision
The short answer
@NameBinding is a meta-annotation: we define our own annotation such as @Audited, mark it with @NameBinding, and put it on both the filter class and the resource methods or classes the filter should apply to. DynamicFeature.configure(ResourceInfo, FeatureContext) runs once per resource method at deployment and can register a filter for that method based on any condition we like. Inside a request filter, requestContext.abortWith(response) stops processing so the resource method never runs, but the response filters still run on the abort response.
What to remember
In this answer, we saw two ways to apply a filter selectively. Name binding uses a custom annotation marked @NameBinding on both the filter and the resource methods. DynamicFeature.configure runs once per resource method at deployment and registers filters conditionally, for example with an annotation value. abortWith stops the request so later request filters and the resource method do not run, while response filters still run on the abort response. Pre-matching filters cannot be name-bound, unbound filters are global, and reader and writer interceptors can be bound the same way for compression, signing or body logging.
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.
@NameBinding is a meta-annotation: we define our own annotation such as @Audited, mark it with @NameBinding, and put it on both the filter class and the resource methods or classes the filter should apply to. DynamicFeature.configure(ResourceInfo, FeatureContext) runs once per resource method at deployment and can register a filter for that method based on any condition we like. Inside a request filter, requestContext.abortWith(response) stops processing so the resource method never runs, but the response filters still run on the abort response.
Why this question is asked
Most filters start global: logging, request ids, authentication. Sooner or later we need one that applies only to some endpoints, such as an audit trail for admin operations or a stricter rate limit for expensive searches. Checking the path inside a global filter works but scatters rules through string comparisons. Interviewers ask this question to see whether we know the two binding mechanisms JAX-RS provides and how a filter can stop a request cleanly. In this answer, we will recap global filters, build a name-bound filter, then a DynamicFeature that registers a filter conditionally, explain abortWith step by step, and finish with the limits and the related interceptors.
1A Quick Recap of Global Filters
A filter registered as a provider without any binding is global: it runs for every request that matches a resource method. That is right for cross-cutting concerns that truly apply everywhere. For selective behaviour, JAX-RS gives us two tools. Name binding is static and declarative: an annotation connects filter and methods. DynamicFeature is programmatic: code decides, at deployment time, which methods get which filters.
2Name Binding With a Custom Annotation
We start by defining the annotation. The @NameBinding on it is what turns an ordinary annotation into a binding.
Notice that the annotation must have runtime retention, otherwise JAX-RS cannot see it. We can also put @Audited on the resource class, and then every method in it is bound. If a filter carries two binding annotations, it only applies to methods that have both, which is easy to forget.
3DynamicFeature for Conditional Registration
Name binding cannot look at values. If we want a rate limit filter whose limit depends on an annotation attribute, or a filter applied to every method returning a certain type, we use DynamicFeature. JAX-RS calls configure() once for each resource method during deployment, passing a ResourceInfo that tells us the class and method, and a FeatureContext on which we can register providers for that method only.
Each method gets its own filter instance with its own limit, which name binding could not express. Because configure() runs at deployment, there is no per-request cost for deciding; the decision is already made.
4How abortWith Works
A request filter can decide the request should not continue, for example because the limit is reached. It calls requestContext.abortWith(response). Here is what happens next, step by step.
- Pre-matching request filtersrun in priority order
- Match the resource methodname-bound and dynamic filters are now known
- Post-matching request filtersglobal and bound, in priority order
- Did a filter call abortWith?YesSkip the rest and the resourceNoRead entity, invoke resource method
- Response filtersrun on the abort response or the real one
- Write the responsestatus, headers, entity
6 steps in order. Play them, or step through them one at a time.
The remaining request filters are skipped, the resource method does not run, and no request entity is read. But the response filters do run on the abort response, which is why the request id header still appears. This is useful, because headers such as CORS, request ids and security headers stay consistent on error responses, and it is also a trap: a response filter that assumes a resource ran, for example one that reads a property the resource set, must cope with that property being absent. Note also that abortWith does not throw; the filter should return right after calling it.
5Limits: Pre-Matching Filters and Global Behaviour
A @PreMatching filter runs before JAX-RS knows which resource method will handle the request, so there is nothing to bind it to. Name binding annotations on a pre-matching filter are therefore ignored, and DynamicFeature only applies to post-matching providers. A filter with no binding applies to all matched requests.
The same binding works for the other provider types that wrap entity streams: ReaderInterceptor and WriterInterceptor. A @Compressed name-bound WriterInterceptor can gzip only selected responses, and a bound ReaderInterceptor can verify a signature on the body of selected requests, or log bodies only for a debug endpoint.
6Follow-up questions
When would you choose DynamicFeature over name binding?
When the decision depends on something more than presence of an annotation: an annotation attribute, the return type, the HTTP method, or configuration such as a feature flag read at startup. Name binding is simpler to read, so I use it when a marker annotation is enough.
Can a name-bound filter apply to a whole Application?
Yes. If the binding annotation is placed on the Application subclass, the filter applies to every resource method in that application, which is effectively global but still explicit. That is occasionally useful when several applications share one set of providers.
Does throwing an exception from a filter work the same as abortWith?
Throwing a WebApplicationException also stops processing and goes through exception mappers to produce a response. abortWith is more direct and avoids exception handling, so it is usual for expected outcomes like 401 or 429, while exceptions are for unexpected failures.
7Conclusion
In this answer, we saw two ways to apply a filter selectively. Name binding uses a custom annotation marked @NameBinding on both the filter and the resource methods. DynamicFeature.configure runs once per resource method at deployment and registers filters conditionally, for example with an annotation value. abortWith stops the request so later request filters and the resource method do not run, while response filters still run on the abort response. Pre-matching filters cannot be name-bound, unbound filters are global, and reader and writer interceptors can be bound the same way for compression, signing or body logging.
Open with
There are two mechanisms. Name binding is declarative: I define an annotation such as Audited, mark it with @NameBinding and runtime retention, and put it on the filter class and on the resource methods or classes it should apply to.
Then, if they want more
JAX-RS then only runs that filter for those methods.
If the decision needs logic, I use a DynamicFeature: its configure method gets a ResourceInfo and a FeatureContext and is called once per resource method at deployment, so I can read an annotation value, say a rate limit of 30 per minute, and register a filter instance configured for just that method.
For abortWith: when a request filter calls requestContext.abortWith with a Response, the remaining request filters and the resource method are skipped and no entity is read, but the response filters still run on that response, so things like request id or CORS headers are still added, and those filters must not assume the resource ran.
A couple of limits: PreMatching filters run before matching, so they cannot be name-bound, and an unbound filter applies to all matched requests. The same binding works for reader and writer interceptors, which wrap the entity streams, for compression or signing.
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
- Jakarta EE specifications
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 "Explain how to apply a filter only to some resource methods in JAX-RS using @NameBinding...": https://scrivio-interview-library.netlify.app/java/jax-rs/5f1bc6166b/