在 Spring WebFlux 中进行异步 SOAP 调用

Chr*_*ker 5 java soap spring-ws spring-webflux spring-reactive

我有一个使用 WebFlux 和 REST API 的反应式 Spring 应用程序。每当用户调用我的 API 时,我都需要调用公开 WSDL 的 SOAP 服务,执行一些操作并返回结果。

如何将对 SOAP 服务的调用与 Reactive WebFlux 框架结合起来?

在我看来,我可以通过两种不同的方式来做到这一点:

  1. 使用 WebFlux 的 WebClient 构造并发送 SOAP 消息。
  2. 使用 Mono / Flux 中的 WebServiceGatewaySupport 包装同步调用。

第一种方法是我的偏好,但我不知道该怎么做。

这里也提出了类似的问题: Reactive Spring WebClient - Making a SOAP call,它引用了这篇博客文章(https://blog.godatadriven.com/jaxws-reactive-client)。但我无法让这个例子发挥作用。

在 Gradle 插件中使用,wsdl2java我可以使用异步方法创建客户端界面,但我不明白如何使用它。使用时WebServiceGatewaySupport,我根本不使用生成的接口或其方法。相反,我调用通用marshalSendAndReceive方法

public class MySoapClient extends WebServiceGatewaySupport {

    public QueryResponse execute() {
        Query query = new ObjectFactory().createQuery();
        // Further create and set the domain object here from the wsdl2java generated classes       
        return (QueryResponse) getWebServiceTemplate().marshalSendAndReceive(query);
    }
}
Run Code Online (Sandbox Code Playgroud)

谁能分享一个从 WebFlux 控制器到进行 SOAP 调用并异步返回的完整示例吗?我觉得我错过了一些重要的东西。

Nik*_*tov 0

我面临同样的问题一周了,但仍然找不到最好的解决方案。如果您想测试 WebClient,您只需使用 SOAP Envelope 请求发布一个字符串。像这样的东西:

    String _request = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
              "<soap:Body>\n" +
               "<request>\n" +
                  "<Example>blabla</Example>\n" +
               "</request>\n" +
              "</soap:Body>\n" +
            "</soap:Envelope>";

    WebClient webClient = WebClient.builder().baseUrl("http://example-service").build();

    Mono<String> stringMono = webClient.post()
            .uri("/example-port")
            .body(BodyInserters.fromObject(_request))
            .retrieve()
            .bodyToMono(String.class);

    stringMono.subscribe(System.out::println);
Run Code Online (Sandbox Code Playgroud)

问题是您需要弄清楚如何将整个 SOAP Envelope(请求和响应)序列化为字符串。这只是一个示例 - 不是解决方案。