Types that can be injected with @Context in JAX-RS 2.0
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:
javax.ws.rs.core.Application
javax.ws.rs.core.HttpHeaders
javax.ws.rs.core.Request
javax.ws.rs.core.SecurityContext
javax.ws.rs.core.UriInfo
javax.ws.rs.core.Configuration
javax.ws.rs.container.ResourceContext
javax.ws.rs.ext.Providers
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:
javax.servlet.HttpServletRequest
javax.servlet.HttpServletResponse
javax.servlet.ServletConfig
javax.servlet.ServletContext
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 theApplication
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 theResponse
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 ofSecurityContext
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
Configuration
s 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.