java 如何从 Jersey 的 ContainerRequest 中提取请求属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16911131/
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
How can I extract request attributes from Jersey's ContainerRequest?
提问by Dejell
HttpServletRequest
has a method setAttribute(String, Object)
.
HttpServletRequest
有方法setAttribute(String, Object)
。
How can I extract this attribute from ContainterRequest
?
如何从中提取此属性ContainterRequest
?
I didn't find: getAttribute
method!
我没有找到:getAttribute
方法!
Code
代码
public class AuthenticationFilter implements Filter {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) servletRequest;
// .... ....
httpReq.setAttribute("businessId", businessId);
}
}
In Jersey Filter:
在泽西过滤器中:
private class Filter implements ResourceFilter, ContainerRequestFilter {
public ContainerRequest filter(ContainerRequest request) {
// ..extract the attribute from the httpReq
}
}
回答by Ryan Stewart
You can't. They're not exposed through the Jersey API in any way. If you search the Jersey codebase, you'll find that there are no uses of HttpServletRequest.getAttributeNames()
, which you'd expect to be used if they were being copied en masse. You'll also find that there are only a handful of uses of HttpServletRequest.getAttribute()
, and it's strictly for internal bookkeeping.
你不能。它们不会以任何方式通过 Jersey API 公开。如果您搜索 Jersey 代码库,您会发现没有使用HttpServletRequest.getAttributeNames()
,如果它们被大量复制,您希望使用它们。您还会发现 的用途屈指可数HttpServletRequest.getAttribute()
,而且严格用于内部簿记。
Note, however, that when deployed in a Servlet Context, JAX-RS allows you to inject the original HttpServletRequest using the @Context
annotation. I'm not certain whether you can do this in a Jersey filter, but it works in MessageBodyReaders/Writers and in resource classes.
但是请注意,在 Servlet 上下文中部署时,JAX-RS 允许您使用@Context
注释注入原始 HttpServletRequest 。我不确定您是否可以在 Jersey 过滤器中执行此操作,但它适用于 MessageBodyReaders/Writers 和资源类。
Update:I've checked, and you can, in fact, inject the HttpServletRequest into a Jersey ContainerRequestFilter by simply including:
更新:我已经检查过,事实上,您可以通过简单地将 HttpServletRequest 注入 Jersey ContainerRequestFilter:
@Context private HttpServletRequest httpRequest;
回答by huljas
I got the @Context
working, but have the problem is that my ContainerRequestFilter
is singleton.
我开始@Context
工作了,但问题是我ContainerRequestFilter
是单身。
I had to implement a custom javax.servlet.Filter
and use a ThreadLocal
to store the HttpServletRequest
.
我必须实现一个自定义javax.servlet.Filter
并使用 aThreadLocal
来存储HttpServletRequest
.
回答by Steven Benitez
If you're using Jersey 2, which implements JAX-RS 2.0, you can implement a ContainerRequestFilter
which defines a filter method as follows:
如果您使用的是 Jersey 2,它实现了 JAX-RS 2.0,您可以实现 a ContainerRequestFilter
,它定义了一个过滤器方法,如下所示:
public void filter(ContainerRequestContext requestContext) throws IOException;
public void filter(ContainerRequestContext requestContext) throws IOException;
ContainerRequestContext
has getProperty(String)
and setProperty(String, Object)
methods, which in a Servlet environment (ServletPropertiesDelegate
), map to the servlet request's getAttribute(String)
and setAttribute(String, Object)
methods.
ContainerRequestContext
hasgetProperty(String)
和setProperty(String, Object)
methods,在 Servlet 环境 ( ServletPropertiesDelegate
) 中,映射到 servlet 请求的getAttribute(String)
和setAttribute(String, Object)
方法。
See: Jersey on GitHub
请参阅:GitHub 上的 Jersey
回答by user7294900
I wanted to add to previous answers my solution, in addition to adding context:
除了添加上下文之外,我还想将我的解决方案添加到以前的答案中:
@Context
private HttpServletRequest httpRequest;
You should set
and get
attributes from the session.
您应该从会话set
和get
属性中获取。
Set:
放:
httpRequest.getSession().setAttribute("businessId", "yourId");
Get:
得到:
Object attribute = httpRequest.getSession().getAttribute("businessId");