@Context HttpServletRequest范围在Jersey ContainerResponseFilter [英] @Context HttpServletRequest scope in Jersey ContainerResponseFilter

查看:837
本文介绍了@Context HttpServletRequest范围在Jersey ContainerResponseFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Jersey响应过滤器。我使用泽西1.17。我想访问过滤器API中的httpServletRequest的一些属性。我现在做的方式如下。是否可以安全地注入servletRequest类似下面的代码段,否则会导致某种并发问题?如果有多个请求并发进来,servletRequest在不同的请求中是否会相互覆盖?感谢您的帮助。

I am writing an Jersey Response filter. I am using Jersey 1.17. I want to get access to some attributes of the httpServletRequest in the filter API. The way what i am doing right now is as below. Is it safe to inject the servletRequest like in the snippet below or will this cause some kind of concurrency issues? If there are multiple requests coming in conncurrently, will the servletRequest in different requests overwrite each other? Thanks for your hlep.

public class LoggingFilter implements ContainerResponseFilter {
@Context private HttpServletRequest servletRequest;
@Override
public ContainerResponse filter(final ContainerRequest req, final ContainerResponse resp) {
String s =  this.servletRequest.getAttribute("xxx");
....
}
}


推荐答案

第9.1节(最新,5.1以前)并发性 JAX-RS规范说明:

Section 9.1 (latest, 5.1 previously) Concurrency of the JAX-RS specification states:


上下文特定于特定请求,
JAX-RS组件(提供程序和资源类,生命周期
而不是每个请求)可能需要支持多个并发
请求。当注入
Section 9.2中列出的类型的实例时,提供的实例必须能够为特定请求选择
正确的上下文。使用线程本地代理是
实现这一点的常见方式。

Context is specific to a particular request but instances of certain JAX-RS components (providers and resource classes with a lifecycle other than per-request) may need to support multiple concurrent requests. When injecting an instance of one of the types listed in Section 9.2, the instance supplied MUST be capable of selecting the correct context for a particular request. Use of a thread-local proxy is a common way to achieve this.

因此,根据规范,JAX -RS实现(例如Jersey),以确保上下文是安全的。继续做你正在做的事。

So, as per the specification, JAX-RS implementations (e.g. Jersey) are required to ensure that the context is safe. Keep doing what you're doing.

另请参阅:从Jersey的容器请求中提取请求属性

这篇关于@Context HttpServletRequest范围在Jersey ContainerResponseFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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