Java 我可以使用 @Context 注释注入哪些对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20937362/
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
What objects can I inject using the @Context annotation?
提问by idrosid
I'm new to JAX-RS and I'm trying to understand how the @Context
annotation is supposed to work.
我是 JAX-RS 的新手,我试图了解@Context
注释应该如何工作。
At the javadocthere is a list of six classes (Application
, UriInfo
, Request
, HttpHeaders
, SecurityContext
, Providers
). However I find code on the web that use the this annotation with other types, for example:
在javadoc 中有六个类的列表(Application
, UriInfo
, Request
, HttpHeaders
, SecurityContext
, Providers
)。但是我在网上找到了将 this 注释用于其他类型的代码,例如:
@GET
public String something(@Context HttpServletRequest req) {
}
Is there a list of supported types that can be used with this annotations? Does this list change between implementation of the standard?
是否有可与此注释一起使用的受支持类型列表?此列表在标准实施之间是否会发生变化?
I'm currently experimenting with Jersey and I'm worried that I'll write code that cannot be ported to other JAX-RS implementation.
我目前正在试验 Jersey,我担心我会编写无法移植到其他 JAX-RS 实现的代码。
采纳答案by Vidya
The riveting JAX-RS specificationdefines all the standard types you can inject via @Context
.
引人入胜的 JAX-RS规范定义了您可以通过@Context
.
But if I were you, I would just consult the specific documentation of your chosen provider to see what is available.
但是,如果我是您,我只会查阅您选择的提供商的特定文档,以查看可用的内容。
For example, RESTEasy provides these valuesvia @Context
. Meanwhile, Jersey provides these. Obviously there will be overlap because of the standard context values.
回答by cassiomolin
The @Context
annotation allows you to inject request/response context details into JAX-RS provider and resource classes. Injection can be performed into a class field, a bean propertyor a method parameter.
该@Context
注解允许你注入的请求/响应方面的细节到JAX-RS提供者和资源类。可以在类字段、bean 属性或方法参数中执行注入。
The following list summarizes all the types that can be injected using the @Context
annotation, according to the JAX-RS 2.0 specification:
@Context
根据JAX-RS 2.0 规范,以下列表总结了可以使用注释注入的所有类型:
javax.ws.rs.core.Application
javax.ws.rs.core.HttpHeaders
javax.ws.rs.core.Request
javax.ws.rs.core.SecurityContext
javax.ws.rs.core.UriInfo
javax.ws.rs.core.Configuration
javax.ws.rs.container.ResourceContext
javax.ws.rs.ext.Providers
javax.ws.rs.core.Application
javax.ws.rs.core.HttpHeaders
javax.ws.rs.core.Request
javax.ws.rs.core.SecurityContext
javax.ws.rs.core.UriInfo
javax.ws.rs.core.Configuration
javax.ws.rs.container.ResourceContext
javax.ws.rs.ext.Providers
Except for Configuration
and Providers
, which
are injectable in both client and server-side providers, all the other types are server-side only.
除了Configuration
和Providers
,这是在客户端和服务器端提供注射,所有其他类型是唯一的服务器端。
The following types are available onlywhen the application is deployed in a servlet container:
以下类型仅在应用程序部署在 servlet 容器中时可用:
javax.servlet.HttpServletRequest
javax.servlet.HttpServletResponse
javax.servlet.ServletConfig
javax.servlet.ServletContext
javax.servlet.HttpServletRequest
javax.servlet.HttpServletResponse
javax.servlet.ServletConfig
javax.servlet.ServletContext
JAX-RS 2.1introduced other types that can be injected with @Context
:
JAX-RS 2.1引入了可以注入的其他类型@Context
:
Besides the standard types listed above, JAX-RS implementations, such as Jersey, RESTEasyand Apache CXF, might define their own types that can be injected using @Context
.
除了上面列出的标准类型之外,JAX-RS 实现(例如Jersey、RESTEasy和Apache CXF)可能会定义自己的类型,可以使用@Context
.
Find below a quick description of each JAX-RS type available for injection:
在下面找到可用于注入的每种 JAX-RS 类型的快速描述:
Application:The instance of the application-supplied
Application
subclass can be injected into a class field or method parameter. Access to theApplication
subclass instance allows configuration information to be centralized in that class.URIs and URI templates:
UriInfo
provides both static and dynamic, per-request information, about the components of a request URI.Headers:
HttpHeaders
provides access to request header information either in map form or via strongly typed convenience methods. Response headers may be provided using theResponse
class.Content negotiation and preconditions:The methods of
Request
allow a caller to determine the best matching representation variant and to evaluate whether the current state of the resource matches any preconditions in the request.Security context:The
SecurityContext
interface provides access to information about the security context of the current request. The methods ofSecurityContext
provide access to the current user principal, information about roles assumed by the requester, whether the request arrived over a secure channel and the authentication scheme used.Providers:The
Providers
interface allows for lookup of provider instances based on a set of search criteria. This interface is expected to be primarily of interest to provider authors wishing to use other providers functionality. It is injectable in both client and server providers.Resource context:The
ResourceContext
interface provides access to instantiation and initialization of resource or subresource classes in the default per-request scope. It can be injected to help with creation and initialization, or just initialization, of instances created by an application.Configuration:Both the client and the server runtime
Configuration
s are available for injection in providers (client or server) and resource classes (server only).SSE events:
SseEventSink
represents the incoming SSE connection and provides methods to send events.Sse
provides factory methods for events and broadcasters.
应用程序:应用程序提供的
Application
子类的实例可以注入到类字段或方法参数中。对子Application
类实例的访问允许将配置信息集中在该类中。URI 和 URI 模板:
UriInfo
提供关于请求 URI 组件的静态和动态的每个请求信息。标头:
HttpHeaders
以地图形式或通过强类型便利方法提供对请求标头信息的访问。可以使用Response
该类提供响应头。内容协商和前提:的方法
Request
允许呼叫者,以确定最佳匹配表示变种,并评估资源的当前状态是否在请求任何先决条件相匹配。安全上下文:该
SecurityContext
接口提供对有关当前请求的安全上下文的信息的访问。SecurityContext
提供对当前用户主体的访问的方法、有关请求者承担的角色的信息、请求是否通过安全通道到达以及使用的身份验证方案。提供者:该
Providers
接口允许根据一组搜索条件查找提供者实例。希望使用其他提供者功能的提供者作者主要对这个接口感兴趣。它可以在客户端和服务器提供程序中注入。资源上下文:该
ResourceContext
接口提供对默认每个请求范围内的资源或子资源类的实例化和初始化的访问。可以注入它来帮助创建和初始化,或者只是初始化由应用程序创建的实例。配置:客户端和服务器运行时
Configuration
都可以在提供者(客户端或服务器)和资源类(仅限服务器)中注入。SSE 事件:
SseEventSink
表示传入的 SSE 连接并提供发送事件的方法。Sse
为事件和广播公司提供工厂方法。
This postwritten by Arjan Tijmssuggests that future versions of JAX-RS may havea stronger integration with CDI. So @Context
may be deprecated and then removed in favor of @Inject
:
这后以书面阿尔扬Tijms表明,JAX-RS的未来版本可能与CDI更强的整合。所以@Context
可能会被弃用,然后被移除以支持@Inject
:
JAX-RS 2.2
For some reason, one that has largely been lost in time, JAX-RS uses its own dependency injection system based on
@Context
instead of CDI's@Inject
. While JAX-RS was updated at the last moment before its initial release to have some level of support for CDI, the fact that JAX-RS resources are not CDI beans has unnecessarily hold back the spec and has caused confusion even since JAX-RS was introduced in EE 6 (2009).This changeover to CDI could possibly happen in 2 steps; in JAX-RS 2.2 everything that can now be injected by
@Context
should also be injectable using@Inject
and JAX-RS resources would be CDI beans by default (perhaps unless explicitly disabled). At the same time@Context
would be deprecated. In JAX-RS 3.0@Context
would then be actually removed.
JAX-RS 2.2
出于某种原因,JAX-RS 使用自己的依赖注入系统
@Context
而不是 CDI 的@Inject
. 虽然 JAX-RS 在其初始发布前的最后一刻进行了更新,以对 CDI 提供某种程度的支持,但 JAX-RS 资源不是 CDI bean 的事实不必要地阻碍了规范,并且即使自 JAX-RS在 EE 6 (2009) 中引入。这种向 CDI 的转换可能分两步进行;在 JAX-RS 2.2 中,现在可以注入的所有内容也都
@Context
应该是可注入的@Inject
,默认情况下 JAX-RS 资源将是 CDI bean(也许除非明确禁用)。同时@Context
将被弃用。在 JAX-RS 3.0@Context
中,实际上将被删除。
回答by Alex Theedom
The @Context annotation can be used to inject 12 objects. Here is a quick summary of each of them
@Context 注解可用于注入 12 个对象。以下是其中每一项的快速摘要
- HttpHeaders - HTTP header valuesand parameters
- UriInfo - URI query parameters and path variables
- SecurityContext - Gives access to security-related data for the given HTTP request
- Request - Allows precondition request processing
- ServletConfig - The ServletConfig
- ServletContext - The ServletContext
- HttpServletRequest - The HttpServletRequest instance for the request
- HttpServletResponse - The HttpServletResponse instance
- Application, Configuration, and Providers -> Provide information about the JAX-RS application, configuration and providers
- ResourceContext - Gives access to resource class instances
- HttpHeaders - HTTP 标头值和参数
- UriInfo - URI 查询参数和路径变量
- SecurityContext - 授予对给定 HTTP 请求的安全相关数据的访问权限
- 请求 - 允许前置条件请求处理
- ServletConfig - ServletConfig
- ServletContext - ServletContext
- HttpServletRequest - 请求的 HttpServletRequest 实例
- HttpServletResponse - HttpServletResponse 实例
- 应用程序、配置和提供程序 -> 提供有关 JAX-RS 应用程序、配置和提供程序的信息
- ResourceContext - 授予对资源类实例的访问权限
All of these instances can be injected in the resource method
所有这些实例都可以注入到资源方法中
@Path("/")
public class EndpointResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllHttpHeaders(final @Context HttpHeaders httpHeaders){
// Code here that uses httpHeaders
}
}
or as a field:
或作为一个字段:
@Path("/")
public class EndpointResource {
private final @Context HttpHeaders httpHeaders;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllHttpHeaders(){
// Code here that uses httpHeaders
}
}
Here is a five part series answering the question What is @Conext used for?
这是一个由五部分组成的系列,回答了@Conext 的用途是什么?
回答by czerny
To extend already provided answers: JAX-RS implementations usually provide a way to access the context. For RESTEasy it's ResteasyContext
. The context can be listed by
扩展已经提供的答案:JAX-RS 实现通常提供一种访问上下文的方法。对于 RESTEasy,它是ResteasyContext
. 上下文可以通过以下方式列出
ResteasyContext.getContextDataMap()
Keys of returned map can look like:
返回地图的键可能如下所示:
interface javax.ws.rs.core.Request
interface javax.ws.rs.core.HttpHeaders
interface javax.ws.rs.ext.Providers
class org.jboss.resteasy.plugins.server.Cleanables
class org.jboss.resteasy.core.PostResourceMethodInvokers
class org.jboss.resteasy.core.InternalDispatcher
interface javax.ws.rs.container.ResourceInfo
interface io.vertx.core.http.HttpServerResponse
interface io.vertx.core.Context
interface org.jboss.resteasy.spi.Registry
interface org.jboss.resteasy.spi.HttpRequest
interface org.jboss.resteasy.spi.ResteasyDeployment
interface javax.ws.rs.container.ResourceContext
interface org.jboss.resteasy.spi.Dispatcher
interface io.vertx.ext.web.RoutingContext
interface io.vertx.core.http.HttpServerRequest
interface io.vertx.core.Vertx
interface javax.ws.rs.core.Configurable
interface org.jboss.resteasy.spi.ResteasyAsynchronousContext
interface javax.ws.rs.core.SecurityContext
interface javax.ws.rs.core.Configuration
interface javax.ws.rs.core.UriInfo
interface org.jboss.resteasy.spi.HttpResponse