Java API for RESTful Web Services (JAX-RS) provides different types of context to resource classes and providers. You can use context objects to access request information such as discovering the HTTP headers that are sent as part of the request. Context objects also provide convenience methods for evaluating a request and building an appropriate response.
Representational State Transfer (REST) application resources might have a need to inspect some application context data upon invocation. For example, a resource method that processes an HTTP GET query might want to inspect the HTTP headers of the request for the Accept-Language HTTP header so that the method can output a response in the language specified by the request.
JAX-RS defines a simple way to retrieve this data within the scope of the application resource. By declaring the @Context annotation with the appropriate object as a parameter to a resource method or as a field within the resource class, the data that you want is injected into the resource. The JAX-RS implementation populates the parameter or field with the contextual data, and the resource method has access to all the contextual data it needs.
Interface types | Description |
---|---|
javax.ws.rs.core.UriInfo | The UriInfo interface provides the complete URI specified by the request. This interface can also inspect which resource(s) matched the request URI. |
javax.ws.rs.core.Request | The Request interface provides information about the request, such as POST or GET. This interface can also evaluate preconditions based on request entity tags. |
javax.ws.rs.core.HttpHeaders | The HttpHeaders interface provides read-only access to all HTTP headers. |
javax.ws.rs.core.SecurityContext | The SecurityContext interface provides read-only information about security, such as authentication scheme or security principal. |
javax.ws.rs.ext.Providers | The Providers interface enables retrieval of ContextResolver, ExceptionMapper, MessageBodyWriter, or MessageBodyReader implementations. |
In addition to the JAX-RS interface types, you can inject web container types, such as javax.servlet.http.HttpServletRequest, using the @Context annotation as described in the JAX-RS specification.
You have implemented context objects to learn more about requests to your JAX-RS web application.