Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What Is javax.ws.rs.core.context? (Part 3)

DZone's Guide to

What Is javax.ws.rs.core.context? (Part 3)

As we explore @Context, let's dive into the Request helper class and see how it helps with precondition request processing.

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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, 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 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 six 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.

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 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:

Further Reading

I have recently published a mini-series of articles focusing on JAX-RS on my blog readlearncode.com. 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.

Java EE Video Course

If you are just starting out and are new to Java EE it can be pretty confusing to get your head around all the APIs. That is why I produced the video course Learning Java Enterprise Edition. During this two-hour course, you will meet all the most important Java EE APIs. With plenty of code examples and demonstrations on how to develop with Java EE, you will soon be on your way to being a Java EE developer.

After the introduction course, you will want to dive deeper into each APIs. There are courses for that as well. You can advance your knowledge of Java EE by learning how to construct RESTful endpoints using the JAX-RS API, then you can learn how to develop a chat application with the WebSocket API and then master JSON with the JSON-Processing API course. Many more courses on the road map, so why not jump in now and give your Java EE career a kick.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
java ee ,context ,annotation ,java ,tutorial

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}