Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can SOAP be used with Spring Reactor's WebClient?

I've managed to build an SSL connection to the sandbox server and to send the object as a serialised XML object by applying the content type MediaType.APPLICATION_XML. However, this is not enough as the target service only supports SOAP and expects the message properly wrapped in an envelope.


        final var webClient = WebClient.builder()
                .baseUrl(fmdConfiguration.getSinglePackUrl())
                .clientConnector(connector)
                .exchangeStrategies(exchangeStrategies)
                .filter(logResponseStatus())
                .filter(logRequest())
                .build();

        return webClient
                .method(GET)
                .contentType(MediaType.APPLICATION_XML)
                .body(BodyInserters.fromObject(request))
                .retrieve()
                .bodyToMono(SinglePackPingResponse.class);

This is the response from the service:

Unable to create envelope from given source because the root element is not named "Envelope"

Unfortunately the the WebClient doesn't support the media type application/soap+xml. When I try to use it, then the WebClient throws the following error:


org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/soap+xml;charset=UTF-8' not supported for bodyType=eu.nmvs.SinglePackPingRequest
    at org.springframework.web.reactive.function.BodyInserters.unsupportedError(BodyInserters.java:300)
like image 860
Andras Hatvani Avatar asked Nov 26 '25 00:11

Andras Hatvani


1 Answers

I use:

private void acceptedCodecs(ClientCodecConfigurer clientCodecConfigurer) {
    clientCodecConfigurer.customCodecs().encoder(new Jackson2JsonEncoder(new ObjectMapper(), TEXT_XML));
    clientCodecConfigurer.customCodecs().decoder(new Jackson2JsonDecoder(new ObjectMapper(), TEXT_XML));
}

and:

  webClient = webClientBuilder
                .baseUrl(baseUrL)
                .filter(logRequest())
                .exchangeStrategies(ExchangeStrategies.builder().codecs(this::acceptedCodecs).build()).build();
like image 145
Jorge Peinado Avatar answered Nov 28 '25 03:11

Jorge Peinado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!