What is javax.ws.rs.core.context? [ Part 3 ]
How to use the @Context annotation
In part 2 of What is javax.ws.rs.core.context? you learned how to use the @Context annotation to retrieve security information from an injected instance of the SecurityContext class and how to use JAX-RS resource class via an instance of ResourceContext.
In this article, you will learn about using the @Context annotation with Request and Configuration, Providers, and Application.
Request precondition processing with the Request class
The java.ws.rs.core package provides a handy helper class called Request that aids with precondition request processing. Let’s jump into an example to see how this works.
@GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public Response updateEmployee(@PathParam("id") int id, @Context Request request, Employee emp) { Employee employee = database.get(id); EntityTag tag = new EntityTag(Integer.toString(employee.hashCode())); Response.ResponseBuilder builder = request.evaluatePreconditions(tag); if (builder != null) { // Preconditions not met so return return builder.build(); } // Preconditions met so update employee employee.salary = emp.salary; return Response.noContent().build(); }
The resource method, updateEmployee(), accepts the employee entity as a parameter as well as its ID and the Request instance. The method retrieves the employee from the database and uses its hash code to generate an ETag. The ETag is evaluated by passing it to the evaluatePreconditions() method of the Request instance. If the preconditions are met not met the method returns, otherwise the employee entity is updated before returning to the caller.
The java.ws.rs.core.Request method has the 6 methods shown below:
evaluatePreconditions() evaluatePreconditions(Date lastModified) evaluatePreconditions(Date lastModified, EntityTag eTag) evaluatePreconditions(EntityTag eTag) String getMethod() Variant selectVariant(List<Variant> variants)
Three interfaces: Configuration, Providers, and Application
There are three interfaces that provide information about the environment in which your JAX-RS application is operating. They are javax.ws.rs.core.Application, javax.ws.rs.core.Configuration and javax.ws.rs.ext.Providers.
The Application instance specifies the components of a JAX-RS application and supplies further data via three methods:
getClasses() getProperties() getSingletons()
The Configuration instance holds data for the configured application context and consists of a range of methods that retrieve data related to properties enabled features and component registration.
The Providers class provides runtime lookup of provider instances. It contains four getter methods that return the context resolver for a given type, the exception manager for a class of exceptions, a message body reader and a message body writer.
What Next?
That is all for part 3, in part 4 of What is javax.ws.rs.core.context? you will learn how to use the @Context annotation to inject instances of classes that are only available when the application is deployed in a servlet container. They are:
- javax.servlet.HttpServletRequest
- javax.servlet.HttpServletResponse
- javax.servlet.ServletConfig
- javax.servlet.ServletContext
Code Repository
The source code for this and all my articles are in the readlearncode_articles Github repository
Reference: | What is javax.ws.rs.core.context? [ Part 3 ] from our JCG partner Alex Theedom at the Read Learn Code blog. |