@Context

The JAX-RS runtime offers different context objects, which can be used for accessing information associated with the resource class, operating environment, and so on. You may find various context objects that hold information associated with the URI path, request, HTTP header, security, and so on. Some of these context objects also provide the utility methods for dealing with the request and response content. JAX-RS allows you to reference the desired context objects in the code via dependency injection. JAX-RS provides the @javax.ws.rs.Context annotation, which injects the matching context object into the target field. You can specify the @Context annotation on a class field, a resource class bean property (the getter method for accessing the attribute), or a method parameter.

The following example illustrates the use of the @Context annotation to inject the javax.ws.rs.core.UriInfo context object into a method variable. The UriInfo instance provides access to the application and request URI information. This example uses UriInfo to read the query parameter present in the requested URI path template, /departments/IT:

@GET 
@Produces(MediaType.APPLICATION_JSON) 
public List<Department> findAllDepartmentsByName( 
  @Context UriInfo  uriInfo){ 
  String deptName =  
    uriInfo.getPathParameters().getFirst("name"); 
  List<Department> depts= findAllMatchingDepartmentEntities 
    (deptName); 
  return depts; 
} 

Here is a list of the commonly used classes and interfaces, which can be injected using the @Context annotation:

  • javax.ws.rs.core.Application: This class defines the components of a JAX-RS application and supplies additional metadata
  • javax.ws.rs.core.UriInfo: This interface provides access to the application and request URI information
  • javax.ws.rs.core.Request: This interface provides a method for request processing, such as reading the method type and precondition evaluation
  • javax.ws.rs.core.HttpHeaders: This interface provides access to the HTTP header information
  • javax.ws.rs.core.SecurityContext: This interface provides access to security-related information
  • javax.ws.rs.ext.Providers: This interface offers the runtime lookup of a provider instance, such as MessageBodyReader, MessageBodyWriter, ExceptionMapper, and ContextResolver
  • javax.ws.rs.ext.ContextResolver<T>: This interface supplies the requested context to the resource classes and other providers
  • javax.servlet.http.HttpServletRequest: This interface provides the client request information for a servlet
  • javax.servlet.http.HttpServletResponse: This interface is used to send a response to a client
  • javax.servlet.ServletContext: This interface provides methods for a servlet to communicate with its servlet container
  • javax.servlet.ServletConfig: This interface carries the servlet configuration parameters
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.34.226