Quick revision
The short answer
With @Context, a JAX-RS resource or provider can receive request information such as UriInfo, HttpHeaders, Request and SecurityContext, runtime objects such as Providers, Application and ResourceContext, and on a servlet container the HttpServletRequest and its friends. A resource is created per request by default, so whatever it receives belongs to that one request. A provider is a singleton shared by all requests, so the runtime injects a proxy that looks up the current request every time we call it.
What to remember
In this answer, we saw that @Context injects runtime objects: request types such as UriInfo, HttpHeaders, Request and SecurityContext, runtime types such as Providers, Application and ResourceContext, and servlet types on a servlet container. In a per-request resource those objects belong to one request. In a singleton provider the runtime injects a thread-bound proxy that resolves to the current request on each call, so copying its values into fields leaks data between requests. Finally, Jakarta REST is moving toward CDI, so we prefer @Inject where it is supported and keep @Context isolated.
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.
With @Context, a JAX-RS resource or provider can receive request information such as UriInfo, HttpHeaders, Request and SecurityContext, runtime objects such as Providers, Application and ResourceContext, and on a servlet container the HttpServletRequest and its friends. A resource is created per request by default, so whatever it receives belongs to that one request. A provider is a singleton shared by all requests, so the runtime injects a proxy that looks up the current request every time we call it.
Why this question is asked
Most JAX-RS code only needs a path parameter or a request body, but sooner or later we need to build a link from the request URI, read a header, or ask who the caller is. That is where @Context comes in, and it is also where subtle bugs come from, because the same field declaration behaves differently in a resource class and in a filter. Interviewers ask this question to see whether we understand object lifetimes in the runtime, not just the annotation. In this answer, we will recap what @Context is, list what we can inject and where it comes from, compare per-request resources with singleton providers, look at the data leak that happens when we copy values out of a proxy, and finish with where Jakarta REST is heading with CDI.
1A Quick Recap of @Context
JAX-RS, now called Jakarta REST, is the Java standard for writing REST endpoints. Jersey and RESTEasy are the two best-known implementations. We write a resource class, a plain class whose methods are mapped to HTTP paths and verbs with @Path, @GET, @POST and so on, and the runtime calls those methods when requests arrive.
@Context is the annotation that asks the runtime to hand us one of its own objects. We can put it on a field, on a setter, on a constructor parameter or on a resource method parameter. The runtime looks at the declared type and supplies the matching object. It is an older and narrower mechanism than full dependency injection: it only knows about a fixed set of runtime types, not our own services.
Notice that both styles work in a resource. The field is filled in after the runtime creates the object, and the parameter is filled in when the method is called.
2What We Can Inject
The specification defines a set of injectable types, and implementations add a few of their own. They fall into two groups. The first group describes the current request: UriInfo gives the request URI, path and query parameters and builders for new links; HttpHeaders gives the headers, cookies, media types and languages; Request helps with conditional requests and content negotiation, such as evaluating an ETag; and SecurityContext tells us the authenticated principal, whether the caller is in a role, and whether the connection is secure.
The second group describes the runtime itself. Providers lets us look up message body readers, writers, exception mappers and context resolvers at runtime. Application is the configured Application subclass. ResourceContext can create and initialise resource instances, which is useful for sub-resource locators. On a servlet container we can also inject HttpServletRequest, HttpServletResponse, ServletContext and ServletConfig. Those servlet types only exist when the application runs inside a servlet container, so code that injects them is tied to that deployment.
| Type | Describes | Typical use |
|---|---|---|
UriInfo | the current request URI | build self links, read query parameters |
HttpHeaders | the current request headers | read Accept-Language, cookies |
| Request | the current request method and preconditions | evaluatePreconditions with an ETag |
SecurityContext | the authenticated caller | getUserPrincipal, isUserInRole |
| Providers | the runtime's registered providers | find a MessageBodyWriter at runtime |
| Application | the configured application | read application properties |
ResourceContext | resource creation | initialise a sub-resource instance |
HttpServletRequest | the servlet request (servlet only) | remote address, session |
3Per-Request Resources: Simple and Safe
By default the runtime creates a new instance of a resource class for every request. That is the per-request lifecycle. The object is built, its @Context fields are filled with the objects for this request, the method runs, and the object is thrown away.
So inside a per-request resource, an injected UriInfo field really belongs to this request. We can read it, keep it in a local variable or pass it to a helper method on the same thread, and nothing surprising happens. This is why the field style in the first example is harmless in a resource class.
4Singleton Providers: The Runtime Injects a Proxy
Providers are different. A provider is a class the runtime uses around our resources: a ContainerRequestFilter, a ContainerResponseFilter, an ExceptionMapper, a MessageBodyReader or MessageBodyWriter. The runtime creates one instance and shares it across every request, on every thread. Resources can be made singletons too, by registering an instance or with an implementation annotation such as Jersey's @Singleton.
A singleton is created once, but requests keep changing. So the runtime cannot simply put this request's UriInfo into the field. Instead it injects a proxy, a stand-in object generated by the runtime that implements UriInfo. Every time we call a method on it, the proxy finds the request currently being processed, usually through thread-local state bound to the request thread, and forwards the call to the real object for that request.
Notice that the field never changes, yet each call returns the right path. That only works while we call the proxy on the thread that is handling the request. In a filter, the ContainerRequestContext parameter is often the clearer choice anyway, because it is passed to us for exactly this request.
5The Leak: Copying Values Out of the Proxy
The proxy protects us only as long as we keep asking it. The bug appears when someone caches what it returned. Here a filter stores the base URI in a field the first time it runs, to avoid computing it again.
Because the filter is a singleton, the field path is shared by all threads. Under load, one request writes its path and another request reads it a moment later. The response then carries another user's data, which is a correctness bug and possibly a privacy incident. The fix is to keep request values in local variables or in the request context (ContainerRequestContext.setProperty), never in fields of a singleton. The same rule applies when handing a proxy to another thread, which is a separate trap we cover in the debugging question about async work.
6Where Jakarta REST Is Heading: CDI
@Context was designed before CDI, Jakarta's general dependency injection standard, was mature. Having two injection models leads to confusing rules about which objects can be injected where. The Jakarta REST project has been moving toward making these runtime objects available through CDI with @Inject, and toward deprecating @Context in a later release. The exact state depends on the release, so check your version: Jakarta REST 3.1 is part of Jakarta EE 10 and 4.0 is part of Jakarta EE 11.
In practice that means two things. Where our runtime supports it, prefer @Inject for our own services and constructor injection where possible. And keep @Context usage in a few obvious places, ideally method parameters, so that moving to the newer model later is a small change.
7Follow-up questions
Can I inject my own service class with @Context?
No. @Context only resolves the runtime's own types and, in some implementations, a few extra ones they register. Our own services should come from CDI with @Inject, from Spring if we use the Spring integration, or from the implementation's injection system, such as HK2 binders in Jersey. Mixing these up is a common source of null fields.
Is field injection into a resource method parameter different from field injection?
For a per-request resource, both give us this request's object. The parameter form is clearer because the dependency is visible in the method signature, and it is the safest form in singletons because the runtime passes the value for exactly the request being handled. For that reason many teams use parameter injection everywhere.
Does the proxy work inside an asynchronous callback?
Not reliably. The proxy finds the current request through state bound to the request thread. On an executor thread there may be no request bound, or a different one, so the call can fail or return another request's data. Copy the values we need, such as the base URI or the principal name, into local variables before handing work to another thread.
8Conclusion
In this answer, we saw that @Context injects runtime objects: request types such as UriInfo, HttpHeaders, Request and SecurityContext, runtime types such as Providers, Application and ResourceContext, and servlet types on a servlet container. In a per-request resource those objects belong to one request. In a singleton provider the runtime injects a thread-bound proxy that resolves to the current request on each call, so copying its values into fields leaks data between requests. Finally, Jakarta REST is moving toward CDI, so we prefer @Inject where it is supported and keep @Context isolated.
Open with
With @Context we can inject the runtime's own objects. For the current request that means UriInfo, HttpHeaders, Request for preconditions and SecurityContext for the caller. For the runtime there is Providers, Application and ResourceContext, and on a servlet container we can also get HttpServletRequest and the other servlet types.
Then, if they want more
The important part is the lifecycle. Resource classes are per request by default, so the object is created for one request and whatever we inject belongs to that request; it is simple and safe. Providers such as filters, exception mappers and message body readers are singletons shared across threads, so the runtime cannot inject one request's object.
It injects a proxy, and each call on the proxy looks up the request bound to the current thread. That works as long as we call it on the request thread.
The bug I watch for is someone caching a value from the proxy in a field of a singleton, because then one user's path or principal can show up in another user's response under load. The same goes for passing the proxy to another thread.
In filters I usually prefer the ContainerRequestContext parameter, and I keep request values in locals or request properties. Finally, Jakarta REST has been moving these objects toward CDI and @Inject, so I keep @Context usage in a few obvious places and check what my version supports.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Jakarta EE 10, Jakarta EE 11
- 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 "What can you inject with @Context in a JAX-RS resource, and what is the difference...": https://scrivio-interview-library.netlify.app/java/jax-rs/9a66821a23/