Network Deployment (Distributed operating systems), v8.0 > End-to-end paths > Web services - RESTful services
Using 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. We 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. JAX-WSRepresentational 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.
We can 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, you can inject web container types, such as javax.servlet.//publib.boulder.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=/ .HttpServletRequest, using the @Context annotation as described in the JAX-RS specification.
Procedure
- Configure the development environment.
- Define the resources in JAX-RS web applications.
- Configure the JAX-RS application.
- Add context fields and parameters to obtain information about requests.
- Assemble JAX-RS web applications.
- Deploy JAX-RS web applications.
Results
You have implemented context objects to learn more about requests to your JAX-RS web application.