Andras Hatvani
Andras Hatvani

Reputation: 4491

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)

Upvotes: 3

Views: 3513

Answers (1)

Jorge Peinado
Jorge Peinado

Reputation: 11

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();

Upvotes: 1

Related Questions