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

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

How can I extract request attributes from Jersey's ContainerRequest?

javajersey

提问by Dejell

HttpServletRequesthas a method setAttribute(String, Object).

HttpServletRequest有方法setAttribute(String, Object)

How can I extract this attribute from ContainterRequest?

如何从中提取此属性ContainterRequest

I didn't find: getAttributemethod!

我没有找到: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 @Contextannotation. 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 @Contextworking, but have the problem is that my ContainerRequestFilteris singleton.

我开始@Context工作了,但问题是我ContainerRequestFilter是单身。

I had to implement a custom javax.servlet.Filterand use a ThreadLocalto 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 ContainerRequestFilterwhich 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;

ContainerRequestContexthas 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.

ContainerRequestContexthasgetProperty(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 setand getattributes from the session.

您应该从会话setget属性中获取。

Set:

放:

 httpRequest.getSession().setAttribute("businessId", "yourId");

Get:

得到:

Object attribute = httpRequest.getSession().getAttribute("businessId");