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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand
  • A Systematic Approach for Java Software Upgrades
  • From J2EE to Jakarta EE
  • Limited Conversations With Distributed Systems

Trending

  • Google Cloud KMS vs. AWS KMS: Java Encryption Guide
  • Share Delta Lake Tables Between Databricks and Snowflake
  • Why Your GPU Fleet Is Both Full and Idle: Building a Capacity Orchestration Layer
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. JAX-RS: What Is @Context?

JAX-RS: What Is @Context?

When using JAX-RS, we often need to make use of the @Context annotation. But what exactly does it do? And what separates it from @Inject.

By 
Alex Theedom
·
Aug. 25, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
82.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The JAX-RS API from the Java EE ecosystem of technologies provides the annotation @Context, to inject 12 object instances related to the context of HTTP requests. It behaves just like the @Inject and @Autowired annotations in Java EE and Spring respectively.

The object instances that it can inject are the following:

  • SecurityContext – Security context instance for the current HTTP request
  • Request – Used for setting precondition request processing
  • Application, Configuration, and Providers -> Provide access to the JAX-RS application, configuration, and providers instances
  • ResourceContext – Resource context class instances
  • ServletConfig – The ServletConfig instance instance
  • ServletContext – The ServletContext instance
  • HttpServletRequest – The HttpServletRequest instance for the current request
  • HttpServletResponse – The HttpServletResponse instance for the current request
  • HttpHeaders – Maintains the HTTP header keys and values
  • UriInfo – Query parameters and path variables from the URI called

It is a little confusing to have both an @Inject and @Context when both do the same job of injecting objects, but it is envisioned that future version of Java EE will bring more alignment of annotation use.

Where Is @Context Used?

It can be used to inject any of the above-mentioned instances into an instance field or directly into the resource method as a parameter.

Below is an example of the injecting into a method’s resource method parameter list:

@Path("/")
public class EndpointResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getHeaders(final @Context HttpHeaders httpHeaders){
    // Code here that uses httpHeaders
    }
}


And here’s an example of injection into an instances field:

@Path("/")
public class EndpointResource {

    private final @Context HttpHeaders httpHeaders;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getHeaders(){
        // Code here that uses httpHeaders
    }
}


If you want to know more, take a look at this series of articles answering the question What is @Conext in JAX-RS used for?

Code Repository

The source code for this article is in my GitHub repository. Code for all my articles is in the ReadLearnCode Articles repository.

Java EE

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

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand
  • A Systematic Approach for Java Software Upgrades
  • From J2EE to Jakarta EE
  • Limited Conversations With Distributed Systems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook