gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:53:30 29 4
gpt4 key购买 nike

我目前正在开发基于 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com