I have a WebClient which makes calls a SOAP API on another server.
Originally this call was made using WebServiceTemplate, but the goal is to introduce WebFlow and reactive to improve performance by switching to an asynchronous model.
The WebClient successfully returns a response from the server. However, this is working only with block(), as follows:
WebClient webClient = getConfiguredWebClient(...);
String soapResponse = webcClient.post()
.contentype(Media.TEXT_XML)
.bodyValue("myRequestValue")
.retrieve()
.bodyToMono(String.class)
.block();
If block() is replaced with subscribe(), webClient does not return a value, rather the value from the server is passed to the onNext() method within subscribe(). The value can be saved into an AtomicReference variable, as follows:
AtomicReference<String> atomicReference = new AtomicReference<>();
webcClient.post()
.contentype(Media.TEXT_XML)
.bodyValue("myRequestValue")
.retrieve()
.bodyToMono(String.class)
.subscribe( s -> atomicReference.set(s));
String result = atomicReference.get();
... but, because subscribe() does not block, the atomicReference will always return a null value.
Is there a way to work with WebClient in an asynchronous manner, so that the result can be returned to the calling code.
Thanks