jersey - 在基于 RestEasy 的服务中为 ThreadLocal 存储使用服务器请求和响应过滤器

标签 jersey jax-rs resteasy thread-local

我目前正在开发基于 RESTeasy 的 RESTful 服务。我有一个过滤器类,它用作服务器请求过滤器和服务器响应过滤器(即它实现 ContainerRequestFilterContainerResponseFilter 接口(interface))。

在请求开始时,我使用过滤器将一个对象放入 ThreadLocal .该对象在整个请求中由资源使用。在请求结束时,在发出响应之前,过滤器会从 ThreadLocal 中删除对象.

我的问题是,是否可以保证请求过滤器、资源和响应过滤器都在同一个线程中执行?是否有可能在请求过滤器将对象放入 ThreadLocal 之后? ,不同的线程将执行请求(因此无权访问该对象)?

我确定是这种情况,但后来我看到了 http://jersey.576304.n2.nabble.com/Does-filter-method-of-ContainerRequestFilter-run-in-resource-method-thread-td7582648.html (泽西官方论坛)现在我有疑问了。

最佳答案

javax.ws.rs.container.ContainerRequestContext.setProperty(...)





javax.ws.rs.container.ContainerRequestContext.getProperty(...)



可能是正确的方法。 javadoc 指出:

In a Servlet container, the properties are synchronized with the ServletRequest and expose all the attributes available in the ServletRequest. Any modifications of the properties are also reflected in the set of properties of the associated ServletRequest.

关于jersey - 在基于 RestEasy 的服务中为 ThreadLocal 存储使用服务器请求和响应过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32854944/

相关文章:

java - 在独立应用程序中使用 Jersey 的依赖注入(inject)

java - 同一 WAR 中的多个 JAX-RS 应用程序

java - 带有 Response 参数的方法中的 IllegalStateException

java - 从 CXF 2.7.0 迁移到 JAX-RS 2.0(确切地说是 RestEasy 3.0.1)

apache - 如何在 Jersey、Apache Wink 和 JBoss RESTEasy 之间进行选择?

java - 在 jersey 2.x 中添加标题

mysql - AngularJs Java 和 Jersey 的 404 NullPointerException

java - 在 CXF jax-rs 中禁用多部分缓存

google-app-engine - CDI/Weld 无法执行 RESTEasy 资源的构造函数注入(inject)

java - 如何让 Jersey Web 服务类实现接口(interface)并进行事务处理