java 在 Jersey 中是否可以访问注入的 HttpServletRequest 而不是代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29434312/
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
Is it possible in Jersey to have access to an injected HttpServletRequest, instead of to a proxy
提问by Gerrit
When injecting HttpServletRequest
in Jersey/JAX-RS resources, the injected value is a proxy. E.g.:
当注入HttpServletRequest
Jersey/JAX-RS 资源时,注入的值是一个代理。例如:
@Path("/myResource")
class MyResource {
@Inject
HttpServletRequest request;
...
}
Will inject a Proxy
object for the requested HttpServletRequest
. I need access to the actual HttpServletRequest
instance object, because I want to use some container specific features that are not in the proxied HttpServletRequest
interface.
将为Proxy
请求的HttpServletRequest
. 我需要访问实际的HttpServletRequest
实例对象,因为我想使用代理HttpServletRequest
接口中没有的一些容器特定功能。
Is there a way in jersey to have access to the actual object via injection? I know that in older versions of Jersey you could inject a ThreadLocal<HttpServletRequest>
to achieve this. But that doesn't seem to be supported in jersey 2.15 anymore.
球衣有没有办法通过注入访问实际对象?我知道在旧版本的 Jersey 中,您可以注入 aThreadLocal<HttpServletRequest>
来实现这一点。但这似乎不再在球衣 2.15 中得到支持。
Rationale: My code depends on functionality in org.eclipse.jetty.server.Request
which implements HttpRequest
, and adds functionality for HTTP/2 push. I would like to use that with Jersey/JAX-RS.
理由:我的代码依赖于功能性在org.eclipse.jetty.server.Request
其实现HttpRequest
,并为HTTP / 2推添加功能。我想将它与 Jersey/JAX-RS 一起使用。
回答by Paul Samsotha
Don't make your resource class a singleton. If you do this, there is no choice but to proxy, as the request is in a different scope.
不要让你的资源类成为单例。如果你这样做,除了代理别无选择,因为请求在不同的范围内。
@Singleton
@Path("servlet")
public class ServletResource {
@Context
HttpServletRequest request;
@GET
public String getType() {
return request.getClass().getName();
}
}
With @Singleton
和 @Singleton
C:\>curl http://localhost:8080/api/servlet
com.sun.proxy.$Proxy41
C:\>curl http://localhost:8080/api/servlet
com.sun.proxy.$Proxy41
Without @Singleton
没有 @Singleton
C:\>curl http://localhost:8080/api/servlet
org.eclipse.jetty.server.Request
C:\>curl http://localhost:8080/api/servlet
org.eclipse.jetty.server.Request
There are other ways your class can become a singleton, like registering it as an instance
您的类还有其他方法可以成为单例,例如将其注册为实例
You can aslo inject it as a method parameter. Singleton or not, you will get the actual instance
您也可以将其作为方法参数注入。单例与否,您将获得实际实例
@GET
public String getType(@Context HttpServletRequest request) {
return request.getClass().getName();
}
See Also
也可以看看
回答by Eric Ben
As Op said that
正如 Op 所说
I know that in older versions of Jersey you could inject a ThreadLocal to achieve this.
我知道在旧版本的 Jersey 中,您可以注入 ThreadLocal 来实现这一点。
I just want to add some code here which could implement this for those who are still using an older version of jersey like I do:
我只想在这里添加一些代码,这些代码可以为那些像我一样仍在使用旧版球衣的人实现这一点:
@Context ThreadLocal<HttpServletRequest> treq;
And a link for this:Thread access to @Context objects?Hope that would help.
还有一个链接:Thread access to @Context objects? 希望这会有所帮助。