Understanding REST APIs - What are Context and @Context?
Asked Answered
S

4

8

I recently went through restful web services tutorial, but couldn't understand what a context is. Can someone explain what it it and also what @Context does?

Swiss answered 2/8, 2016 at 11:44 Comment(5)
Can you clarify it? Are you talking about a particular framework?Karolekarolina
@CássioMazzochiMolin yeah, jerseySwiss
And what do you mean with context? What tutorial have you been reading?Karolekarolina
tutorialspoint.com/restful/restful_jax_rs.htm here @Context is mentionedSwiss
That definition is not really clear. Please check my answer.Karolekarolina
D
4

JAX-RS provides the @Context annotation to inject 12 object instances related to the context of the HTTP request and they are:

  • 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 contect aclass 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 (or @Autowired in Spring) and @Context that does the same job, but it is hoped to bring more alignment to Java EE in the next edition. In the meantime, you will have to make do.

An interesting feature is that all of these instances can be injected as a field value or directly into the resource method.

An example of injection into the resource method parameter list:

@Path("/")
public class EndpointResource {

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

An example of injection into a field:

@Path("/")
public class EndpointResource {

  private final @Context HttpHeaders httpHeaders;

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response getAllHttpHeaders(){
      // 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?

Daniels answered 21/8, 2017 at 11:12 Comment(0)
K
0

For an explanation about context in programming terms, have a look at this answer.

The JAX-RS API provides a @Context annotation. In general, such annotation can be used to obtain contextual Java types related to the request or response. Those types can be injected into classes managed by the JAX-RS runtime.

For example, to inject the HttpServletRequest in your resource method, you can do the following:

@GET
public Resonse foo(@Context HttpServletRequest request) {
    ...
}

Additional resources:

Karolekarolina answered 2/8, 2016 at 12:30 Comment(3)
Can't we directly write something like HttpServelet req, instead of putting @Context before it, and also what is a context?Swiss
@Swiss See my updated answer. The @Context annotation is required to inject types such as HttpServletRequest in classes managed by JAX-RS.Karolekarolina
@Swiss Let me know if you need any further details.Karolekarolina
J
0

context is a react Hook feature that helps to pass the data from one component to another without calling the props at each level ... it avoids prop drilling. by defining the provider in one context component and then you can call everywhere and every time when you need.

Joris answered 14/2, 2023 at 7:29 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Grisette
Thank you @Gech. But just to clarify, the question is about REST APIs in Java and not React. This answer, although may have some value to it, it's irrelevant to this question.Gelman
H
-2

REST is an architectural style and one of the way to implement web-services. (Other is SOAP). There are many implementations of REST architecture and one of them in java is Jersey (https://jersey.java.net/) @context is annotation in Jersey framework. It's a class from jax rs jar. (https://jersey.java.net/apidocs-javax.jax-rs/2.0.1/javax/ws/rs/core/Context.html)

Haggai answered 2/8, 2016 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.