如何在 Spring-ws 端点中访问 HTTP 标头?
如何在 Spring-ws 端点中访问 HTTP 标头?
我的代码如下所示:
public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
protected Object invokeInternal(Object arg) throws Exception {
MyReq request = (MyReq) arg;
// need to access some HTTP headers here
return createMyResp();
}
}
invokeInternal()
仅获取未编组的 JAXB 对象作为参数。如何访问 invokeInternal()
中请求附带的 HTTP 标头?
一种可能有效的方法是创建一个 Servlet 过滤器,将标头值存储到 ThreadLocal 变量,然后在 invokeInternal() 内部访问该变量,但是有没有更好、更弹簧的方法-类似的方法吗?
How can I access HTTP headers in Spring-ws endpoint?
My code looks like this:
public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
protected Object invokeInternal(Object arg) throws Exception {
MyReq request = (MyReq) arg;
// need to access some HTTP headers here
return createMyResp();
}
}
invokeInternal()
gets only the unmarshalled JAXB object as the parameter. How can I access HTTP headers that came with the request inside invokeInternal()
?
One way that would probably work is to create a Servlet filter that stores header values to ThreadLocal
variable that is then accessed inside invokeInternal()
, but is there a nicer, more spring-like way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加这些方法。
TransportContextHolder
将在线程局部变量中保存一些与传输(本例中为 HTTP)相关的数据。您可以从TransportContext
。You can add these methods. The
TransportContextHolder
will hold some data related to transport (HTTP in this case) in a thread local variable. You can accessHttpServletRequest
from theTransportContext
.您可以通过注入 HttpServletRequest 来访问 Spring SOAP Endpoint 中的 HTTP 标头。
例如,您需要获取 Autorization 标头(您使用基本身份验证)。
SOAP 请求:
@Endpoint java 类
You can access to HTTP headers in Spring SOAP Endpoint by injecting HttpServletRequest.
For example, you need to get Autorization header (you use Basic authentication).
SOAP request:
@Endpoint java class
我遇到了同样的问题(请参阅此其他问题)。我需要向我的 WS 添加一个 Content-Type 标头。我走上了Servlet Filter的道路。大多数时候,您不需要更改 Web 服务中的 HTTP 标头。但是......有时理论与实践之间存在差异。
I had the same kind of problem (see this other question). I needed to add a Content-Type header to my WS. I went the road of the Servlet Filter. Most of the time, you should not need to change HTTP headers in a webservice. But ... there is sometime a diference between theory and practice.