java 理解 REST APIs - 什么是 Context 和 @Context?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38719533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:42:05  来源:igfitidea点击:

Understanding REST APIs - What are Context and @Context?

javaweb-servicesjersey

提问by user3248186

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

我最近浏览了宁静的 Web 服务教程,但无法理解上下文是什么。有人可以解释它是什么以及@Context 的作用吗?

回答by Alex Theedom

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

JAX-RS 提供了@Context注入 12 个与 HTTP 请求上下文相关的对象实例的注解,它们是:

  • 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
  • SecurityContext- 当前 HTTP 请求的安全上下文实例
  • Request- 用于设置前置条件请求处理
  • ApplicationConfigurationProviders-> 提供对 JAX-RS 应用程序、配置和提供程序实例的访问
  • ResourceContext- 资源联系类实例
  • ServletConfig- ServletConfig 实例实例
  • ServletContext- ServletContext 实例
  • HttpServletRequest- 当前请求的 HttpServletRequest 实例
  • HttpServletResponse- 当前请求的 HttpServletResponse 实例
  • HttpHeaders- 维护 HTTP 标头键和值
  • UriInfo- 从调用的 URI 中查询参数和路径变量

It is a little confusing to have both an @Inject(or @Autowiredin Spring) and @Contextthat 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.

有一个@Inject(或@Autowired在 Spring 中)和@Context它做同样的工作有点令人困惑,但希望在下一版本中为 Java EE 带来更多的一致性。与此同时,你将不得不凑合。

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?

如果您想了解更多,请查看本系列文章回答的问题JAX-RS 中的@Conext 是用来做什么的?

回答by cassiomolin

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

有关编程术语中上下文的解释,请查看此答案

The JAX-RS APIprovides a @Contextannotation. 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.

JAX-RS API提供了@Context注释。通常,此类注释可用于获取与 request 或 response 相关的上下文 Java 类型。这些类型可以注入到由 JAX-RS 运行时管理的类中。

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

例如,要HttpServletRequest在您的资源方法中注入,您可以执行以下操作:

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

Additional resources:

其他资源:

回答by Pranalee

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)

REST 是一种架构风格,也是实现 Web 服务的一种方式。(另一个是 SOAP)。有REST架构的许多实现并在java中其中之一是新泽西州(https://jersey.java.net/)@context是在泽西框架注解。这是来自 jax rs jar 的一个类。( https://jersey.java.net/apidocs-javax.jax-rs/2.0.1/javax/ws/rs/core/Context.html)