Understanding REST APIs - What are Context and @Co

2020-07-11 05:33发布

问题:

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?

回答1:

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?



回答2:

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:

  • Types that can be injected with @Context
  • Jersey documentation about resources


回答3:

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)