Using @Context in JAX-RS [ part 1 ]

JAX-RS provides the @Context annotation to inject a variety of resources in your RESTful services. Some of the most commonly injected components are HTTP headers, HTTP URI related information. Here is a complete list (in no specific order)

  • HTTP headers
  • HTTP URI details
  • Security Context
  • Resource Context
  • Request
  • Configuration
  • Application
  • Providers

Lets look at these one by one with the help of examples

HTTP headers

Although HTTP headers can be injected using the @HeaderParam annotation, JAX-RS also provides the facility of injecting an instance of the HttpHeaders interface (as an instance variable or method parameter). This is useful when you want to iterate over all possible headers rather than injecting a specific header value by name

public class InjectURIDetails{
public void test(@Context HttpHeaders headers){
System.out.println("ALL headers — "+ headers.getRequestHeaders().toString());
System.out.println("'Accept' header — "+ headers.getHeaderString("Accept"));
System.out.println("'TestCookie' value — "+ headers.getCookies().get("TestCookie").getValue());

HTTP URI details

UriInfo is another interface whose instance can be injected by JAX-RS (as an instance variable or method parameter). Use this instance to fetch additional details related to the request URI and its parameters (query, path)

public class InjectURIDetails{
public void test(@Context UriInfo uriDetails){
System.out.println("ALL query parameters — "+ uriDetails.getQueryParameters().toString());
System.out.println("'id' query parameter — "+ uriDetails.getQueryParameters.get("id"));
System.out.println("Complete URI — "+ uriDetails.getRequestUri());


An instance of the Providers interface can be injected using @Context. One needs to be aware of the fact that this is only valid within an existing provider. A Providers instance enables the current Provider to search for other registered providers in the current JAX-RS container.

Note: Please do not get confused between Provider and Providers.


  • A JAX-RS Provider is a is generic term for any class which supplements/extends the JAX-RS features by implementing standard interfaces exposed by the JAX-RS specification
  • It is annotated using the @Provider annotation for automatic discovery by the run time
  • Examples of JAX-RS providers are – Message Body Reader, Message Body Writer, Exception Mapper and Context Providers.


Refers to the (injectable) interface which was discussed in this sub section

Security Context

Inject an instance of the interface (as an instance variable or method parameter) if you want to gain more insight into identity of the entity invoking your RESTful service. This interface exposes the following information

  • Instance of representing the caller
  • Whether or not the user if a part of a specific role
  • Which authentication scheme is being used (BASIC/FORM/DIGEST/CERT)
  • Whether or not the request invoked over HTTPS

public class InjectSecurityContext{
public void test(@Context SecurityContext secContext){
System.out.println("Caller — "+ secContext.getUserPrincipal()getName());
System.out.println("Authentication Scheme — "+ secContext.getAuthenticationScheme());
System.out.println("Over HTTPS ? — "+ secContext.isSecure());
System.out.println("Belongs to 'admin' role? — "+ secContext.isUserInRole("admin");

That’s all for this part. Rest of the injectables will be covered in the next iteration.

Until then.. Cheers!

About Abhishek

Loves Go, NoSQL DBs and messaging systems
This entry was posted in Java, Java EE and tagged , , , . Bookmark the permalink.

7 Responses to Using @Context in JAX-RS [ part 1 ]

  1. Pingback: JavaWeekly 19/15: Jigsaw, Java8 concurrency, vJUG book club

  2. Pingback: Part 1: Using @Context in JAX-RS | Dinesh Ram Kali.

  3. Thanks, nice tip. But could we use @Inject instead of @Context?


    • Abhishek says:

      Glad you like the content. From what I know, its not possible to use @Inject for these items. Better alignment with CDI is a theme for Java EE 8 in general and the same applies for JAX-RS 2.1 (part of Java EE 8). Having said that, CDI works fine with JAX-RS 2.0 in general e.g. injection of simple managed beans. It’s just that injection of the components mentioned in this post are best done using @Context (as of now)


      • Thanks for the answer! Since @Context doesn’t need to specify any extra parameters, It’s good if we could inject in general way using @Inject. I hope it will be possible in future version of JAX-RS.


  4. Pingback: What is [ Part 1 ] Boost your career with us Professional Java EE Video Training and Tutorials

  5. Alex says:

    Hey Abhishek. Great article. It inspired me to dive deeper into the use of the annotation and I found that it is used quite extensively in JAX-RS. So I decided to write a series about this topic and publish it on my blog.

    Cheers. Alex

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s