Using JAX-RS 2.0 context objects to obtain more information about requests
Java API for RESTful Web Services (JAX-RS) 2.0 provides different types of context to application subclasses, root resource classes, and providers. You can use the @Context annotation to inject context objects such as HttpHeaders, UriInfo, HttpServletRequest into class field or method parameter in application subclasses, root resource classes, and providers.
About this task
You can use the following context objects that are available
to providers (client and server), resource classes (server only), and Application
subclasses (server only):
Context object | Type | Description |
---|---|---|
Application | Class | The instance of the application-supplied Application subclass can be injected into a class field or method parameter
using the @Context annotation. Access to the Application
subclass instance allows configuration information to be centralized
in that class. Note: This Application subclass cannot
be injected into the Application subclass itself
since this would create a circular dependency.
|
UriInfo | Interface | The UriInfo interface provides static and dynamic, per-request information, about the components of a request URI. |
HttpHeaders | Interface | The HttpHeaders interface provides access to request header information either in map form or via strongly typed convenience methods. |
Request | Interface | The Request interface allows 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. |
SecurityContext | Interface | The SecurityContext interface provides access to information about the security context of the current request. |
Providers | Interface | The Providers interface allows for lookup of provider instances based on a set of search criteria. |
ResourceContext | Interface | The ResourceContext interface provides access to instantiation and initialization of resource or subresource classes in the default per-request scope. |
Configuration | Interface | Both the client and the server runtime configurations are available for injection via @Context. These configurations are available for injection in providers (client or server) and resource classes (server only). |