News Ticker

What is (Part 3)

How to use the @Context annotation

In part 2 of What is 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 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.

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

    // 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 method has the 6 methods shown below:

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, and

The Application instance specifies the components of a JAX-RS application and supplies further data via three methods:


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.

Code Repository

The source code for this and all my articles are in the readlearncode_articles Github repository.

What Next?

That is all for part 3, in part 4 of What is 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:

Take Java EE Course

New comers to Java EE can get a pretty good intro to the platform with my new video course Learning Java Enterprise Edition. The course is just over two hours and discusses all the most important Java EE APIs including JAX-RS for RESTful APIs, JavaServerFaces, Enterprise Java Beans and much more. There are plenty of code examples and demonstrations on how to develop with Java EE.

If you want to learn more, you can advance your knowledge of Java EE and take a course that teaches you how to develop a RESTful APIs using JAX-RS, you can learn how to develop a fun chat application using the WebSocket API and then become a JSON master with the course on JSON-Processing API. There are many more courses on the way, so why not get in now and give your Java EE career a boost.

Further Reading

I have recently published a mini-series of articles focusing on JAX-RS on my blog The articles focus on and discuss how to best handle bean validation failure, working with MediaTypes, Consumers and Producers, and how to create Resource Entities.

4 Trackbacks / Pingbacks

  1. What is [ Part 2 ] Java EE | Java SE | Design Patterns
  2. What is [ Part 1 ] Boost your career with professional Java EE video course and tutorials. Learn all the technologies required to develop enterprise grade applications. Professional Java EE Video Training and Tutorials
  3. What is [ Part 4 ] Boost your career with professional Java EE video course and tutorials. Learn all the technologies required to develop enterprise grade applications. Professional Java EE Video Training and Tutorials
  4. JAX-RS: What is @Context?

Leave a Reply

%d bloggers like this: