使用 Spring-WS 客户端动态设置自定义 HTTP 标头
使用 Spring-WS 时,如何在客户端动态设置自定义 HTTP 标头(不是 SOAP 标头)?
How do you set a custom HTTP header (not SOAP header) dynamically on the client side when using Spring-WS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
配置:
config:
ClientInterceptor
对于静态标头值非常有效。但是,当每个请求应应用不同的值时,就不可能使用它。在这种情况下,WebServiceMessageCallback
会很有帮助:ClientInterceptor
works great for static header value. But it is not possible to use it when a different value should be applied per each request. In that caseWebServiceMessageCallback
is helpful:当使用 Spring Integration 3 和 Spring Integration-ws 时,可以使用以下代码来处理请求:
拦截器可以通过以下方式连接到出站网关:
When using spring integration 3 and spring integration-ws, the following code can be used for handling the request:
The Interceptor can be connected to the outbound gateway in the following way:
实际上,它是 @Tomasz 答案的更新版本,但提供了新的 Spring-WS API, Java 8 快捷方式,并关心使用单独的方法创建
WebServiceMessageCallback
实例。我相信这是更加明显和自足的。
Actually, it is an updated version of the @Tomasz's answer, but provides a new Spring-WS API, Java 8 shortcuts, and cares about creating a
WebServiceMessageCallback
instance with a separate method.I believe it is more obvious and self-sufficient.
java 1.8 的示例方法:如何添加 HTTP 标头:
说明:
使用 getWebServiceTemplate().marshalSendAndReceive,如下所示:https://spring. io/guides/gs/consuming-web-service/
第一个参数是 URI,第二个参数是随请求一起发送的对象。作为第三个参数,您可以添加为
覆盖
public void doWithMessage
的函数。在发送请求之前调用此方法。您可以在其中访问消息并通过以下方式添加请求标头Example Method with java 1.8: How to add a HTTP header:
Explanation:
Use the getWebServiceTemplate().marshalSendAndReceive as described for example here: https://spring.io/guides/gs/consuming-web-service/
First parameter is the URI, second is the object which shall be send with the request. As third Parameter you can add as function
where you override the
public void doWithMessage
. This method gets called before the request is sent. Within you can access the message and add a request Header throughSpring 的 webServiceTemplate.marshalSendAndReceive(request) 方法内部使用 HttpComponentsMessageSender 通过网络发送 SOAP 消息,并进一步使用 WebServiceConnection 与服务器建立 http 连接。您所要做的就是编写自己的自定义 HttpComponentsMessageSender 并在 postMethod 中设置 cookie。
自定义发送者代码:
Spring 配置:
在此之后,我只需获取 bean webServiceTemplate 并调用 marshalSendAndReceive 方法。因此,每个请求在进行 HTTP 调用之前都会设置其自定义 cookie。
Spring's webServiceTemplate.marshalSendAndReceive(request) method internally uses HttpComponentsMessageSender to send the SOAP message over the network and this further uses WebServiceConnection to make http connection with the server. All you have to do is to write your own custom HttpComponentsMessageSender and set the cookie inside postMethod.
Custome sender code:
Spring Configuration :
After this I simply get bean webServiceTemplate and call marshalSendAndReceive method. So every request will have its custom cookie set before making HTTP call.
以下片段已使用 Spring 4.0 进行了测试。它将
WebServiceMessageCallback
附加到org.springframework.ws.client.core.WebServiceTemplate
The following fragment has been tested with Spring 4.0. It appends a
WebServiceMessageCallback
to aorg.springframework.ws.client.core.WebServiceTemplate