WebServiceTemplate 通过其 WebServiceMessageSender 发送。当 SOAP 目标需要正向代理时,配置该发送器的 HTTP 客户端。

对于可用 Apache HttpClient 5 的 Spring WS 4.1:

var http = org.apache.hc.client5.http.impl.classic.HttpClients.custom()
    .setProxy(new org.apache.hc.core5.http.HttpHost(
        "http", "proxy.example", 8080))
    .addRequestInterceptorFirst(
        new org.springframework.ws.transport.http
            .HttpComponents5ClientFactory.RemoveSoapHeadersInterceptor())
    .build();

var sender = new org.springframework.ws.transport.http
    .HttpComponents5MessageSender(http);
webServiceTemplate.setMessageSender(sender);

webServiceTemplate 是应用程序现有的模板。它的 SOAP 服务 URI 仍是目标;HTTP 客户端持有中间层,在被允许的情况下,可以是从 HTTP 代理列表 中挑选的公开候选。

自定义客户端的构造函数不会添加发送器惯用的头拦截器。组装客户端时要保留它:它会移除由 Apache 请求处理管理的、SOAP 提供的传输头。通过 客户端工厂的默认设置 构建的发送器是另一条保留这一集成的构建路径。

通过自定义客户端自身支持的接口为其添加所需的代理凭据和超时。发送器的 setter 方法不能替代对已提供客户端的配置。

为模板的工作负载保留该客户端,并在关闭时释放自有资源。本示例使用 HttpClient 5 发送器;导入 org.apache.http 的 Spring WS 示例描述的是另一代客户端。

相关文章