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.