如何从Jersey的ContainerRequest中提取请求属性? [英] How can I extract request attributes from Jersey's ContainerRequest?

查看:325
本文介绍了如何从Jersey的ContainerRequest中提取请求属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpServletRequest 有一个方法 setAttribute(String,Object)

如何从 ContainterRequest 中提取此属性?

我没有找到: getAttribute 方法!

代码

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);
    }
}

在泽西过滤器中:

private class Filter implements ResourceFilter, ContainerRequestFilter {
    public ContainerRequest filter(ContainerRequest request) {
        // ..extract the attribute from the httpReq
    }
}


推荐答案

你不能。它们不会以任何方式通过Jersey API公开。如果您搜索Jersey代码库,您会发现没有使用 HttpServletRequest.getAttributeNames(),如果要复制它们,您可能会使用它们。集体。您还会发现 HttpServletRequest.getAttribute()只有少数用途,并且它严格用于内部簿记。

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.

但是,请注意,当部署在Servlet上下文中时,JAX-RS允许您使用 @Context 注释注入原始HttpServletRequest。我不确定你是否可以在Jersey过滤器中执行此操作,但它适用于MessageBodyReaders / Writers和资源类。

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.

更新:我已经检查了,事实上,您可以通过简单地包括以下内容将HttpServletRequest注入Jersey ContainerRequestFilter:

Update: I've checked, and you can, in fact, inject the HttpServletRequest into a Jersey ContainerRequestFilter by simply including:

@Context private HttpServletRequest httpRequest;

这篇关于如何从Jersey的ContainerRequest中提取请求属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆