What objects can I inject using the @Context annotation?

The @Context annotation allows you to inject request/response context details into JAX-RS provider and resource classes. Injection can be performed into a class field, a bean property or a method parameter.


The following list summarizes all the types that can be injected using the @Context annotation, according to the JAX-RS 2.0 specification:

Except for Configuration and Providers, which
are injectable in both client and server-side providers, all the other types are server-side only.

The following types are available only when the application is deployed in a servlet container:

JAX-RS 2.1 introduced other types that can be injected with @Context:

Besides the standard types listed above, JAX-RS implementations, such as Jersey, RESTEasy and Apache CXF, might define their own types that can be injected using @Context.


Find below a quick description of each JAX-RS type available for injection:

  • Application: The instance of the application-supplied Application subclass can be injected into a class field or method parameter. Access to the Application subclass instance allows configuration information to be centralized in that class.

  • URIs and URI templates: UriInfo provides both static and dynamic, per-request information, about the components of a request URI.

  • Headers: HttpHeaders provides access to request header information either in map form or via strongly typed convenience methods. Response headers may be provided using the Response class.

  • Content negotiation and preconditions: The methods of Request allow a caller to determine the best matching representation variant and to evaluate whether the current state of the resource matches any preconditions in the request.

  • Security context: The SecurityContext interface provides access to information about the security context of the current request. The methods of SecurityContext provide access to the current user principal, information about roles assumed by the requester, whether the request arrived over a secure channel and the authentication scheme used.

  • Providers: The Providers interface allows for lookup of provider instances based on a set of search criteria. This interface is expected to be primarily of interest to provider authors wishing to use other providers functionality. It is injectable in both client and server providers.

  • Resource context: The ResourceContext interface provides access to instantiation and initialization of resource or subresource classes in the default per-request scope. It can be injected to help with creation and initialization, or just initialization, of instances created by an application.

  • Configuration: Both the client and the server runtime Configurations are available for injection in providers (client or server) and resource classes (server only).

  • SSE events: SseEventSink represents the incoming SSE connection and provides methods to send events. Sse provides factory methods for events and broadcasters.


This post written by Arjan Tijms suggests that future versions of JAX-RS may have a stronger integration with CDI. So @Context may be deprecated and then removed in favor of @Inject:

JAX-RS 2.2

For some reason, one that has largely been lost in time, JAX-RS uses its own dependency injection system based on @Context instead of CDI’s @Inject. While JAX-RS was updated at the last moment before its initial release to have some level of support for CDI, the fact that JAX-RS resources are not CDI beans has unnecessarily hold back the spec and has caused confusion even since JAX-RS was introduced in EE 6 (2009).

This changeover to CDI could possibly happen in 2 steps; in JAX-RS 2.2 everything that can now be injected by @Context should also be injectable using @Inject and JAX-RS resources would be CDI beans by default (perhaps unless explicitly disabled). At the same time @Context would be deprecated. In JAX-RS 3.0 @Context would then be actually removed.

Leave a Comment