DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. What Is javax.ws.rs.core.context? (Part 3)

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.

Alex Theedom user avatar by
Alex Theedom
·
Aug. 22, 17 · Tutorial
Like (3)
Save
Tweet
Share
6.08K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • javax.servlet.HttpServletRequest
  • javax.servlet.HttpServletResponse
  • javax.servlet.ServletConfig
  • javax.servlet.ServletContext

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.

Java EE application Java (programming language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevSecOps Benefits and Challenges
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts
  • Top Five Tools for AI-based Test Automation
  • Too Many Tools? Streamline Your Stack With AIOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: