在服务器端 JAX-WS Webservice 上使用请求 messageHeader 的 Id 设置 ThreadLocal 的最佳方法是什么
What is the best way to set ThreadLocal with Id from request messageHeader on Server side JAX-WS Webservice
我向客户端公开了一个 JAX-WS Web 服务。当客户端调用它时,messageheader 将填充 UserId。当请求来自客户端时,我正在尝试使用实用程序 class 在 thread local 中设置此值,并在将响应发送回客户端之前将其删除,以便可以从任何地方访问它该线程范围的变量。实现 this.Can 的最佳方法是什么我在服务器端使用 过滤器 或者我必须使用 Jax-Ws 处理程序 来初始化这个 value.And 我们还可以在使用 Jax-ws 处理程序 发送响应之前删除这个值吗?
还有其他实现方式吗?
假设 messageheader 你指的是 SOAP <Header>
(而不是 HTTP header ):
如果您使用 Servlet Filter,要检索和设置本地线程,您必须自己解析 SOAP XML 才能访问 UserId 值.
如果您使用 JAX-WS 处理程序,SOAP 已经为您解析为 object 模型。如果您只需要将 UserId 值传递给应用程序的 Web 服务部分,您可以在处理程序中简单地在 SOAPMessageContext as is the example here. You can also set a thread local as you specified. To remove or clear the threadlocal you could either use a servlet filter or simply check the MESSAGE_OUTBOUND_PROPERTY
on the SOAPMessageContext
in your handler: if it's inbound, your handler is being invoked on the web service request, so you want to read the message header and set the threadlocal value. If it's outbound, your handler is being asked to handle the web service response, so you'll want to remove.
上设置值
我向客户端公开了一个 JAX-WS Web 服务。当客户端调用它时,messageheader 将填充 UserId。当请求来自客户端时,我正在尝试使用实用程序 class 在 thread local 中设置此值,并在将响应发送回客户端之前将其删除,以便可以从任何地方访问它该线程范围的变量。实现 this.Can 的最佳方法是什么我在服务器端使用 过滤器 或者我必须使用 Jax-Ws 处理程序 来初始化这个 value.And 我们还可以在使用 Jax-ws 处理程序 发送响应之前删除这个值吗?
还有其他实现方式吗?
假设 messageheader 你指的是 SOAP <Header>
(而不是 HTTP header ):
如果您使用 Servlet Filter,要检索和设置本地线程,您必须自己解析 SOAP XML 才能访问 UserId 值.
如果您使用 JAX-WS 处理程序,SOAP 已经为您解析为 object 模型。如果您只需要将 UserId 值传递给应用程序的 Web 服务部分,您可以在处理程序中简单地在 SOAPMessageContext as is the example here. You can also set a thread local as you specified. To remove or clear the threadlocal you could either use a servlet filter or simply check the MESSAGE_OUTBOUND_PROPERTY
on the SOAPMessageContext
in your handler: if it's inbound, your handler is being invoked on the web service request, so you want to read the message header and set the threadlocal value. If it's outbound, your handler is being asked to handle the web service response, so you'll want to remove.