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

  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS
  • The Third Culture: Blending Teams With Different Management Models
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  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 user avatar
Alex Theedom
·
Aug. 25, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
82.6K Views

Join the DZone community and get the full member experience.

Join For Free

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

The likes didn't load as expected. Please refresh the page and try again.

  • 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