+

Search Tips   |   Advanced Search

Use JAX-RS context objects to obtain more information about requests

Java API for RESTful Web Services (JAX-RS) provides different types of context to resource classes and providers. Use context objects to access request information such as discovering the HTTP headers 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 we 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.

Use the following interface types that are injectable by the JAX-RS runtime environment:

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, we can inject web container types, such as javax.servlet.http.HttpServletRequest, using the @Context annotation as described in the JAX-RS specification.

  1. Configure the development environment.

  2. Define resources in JAX-RS web applications.

  3. Configure the JAX-RS application.

  4. Add context fields and parameters to obtain information about requests.

  5. Assemble JAX-RS web applications.

  6. Deploy JAX-RS web applications.


Results

You have implemented context objects to learn more about requests to the JAX-RS web application.


Subtopics


Related tasks

  • Obtaining HTTP headers using HttpHeaders objects
  • Obtaining information about URIs using UriInfo objects
  • Evaluating request preconditions using Request objects
  • Determine security information using SecurityContext objects

  • Web services specifications and APIs