SOAP如何与Spring Reactor的WebClient一起使用?

时间:2019-04-18 12:48:00

标签: spring soap spring-webflux project-reactor reactor-netty

我已经设法通过应用内容类型MediaType.APPLICATION_XML建立与沙盒服务器的SSL连接,并将该对象作为序列化XML对象发送。但是,这还不够,因为目标服务仅支持SOAP,并且期望消息正确包装在信封中。


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

这是该服务的响应:

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

不幸的是,WebClient不支持媒体类型application / soap + xml。当我尝试使用它时,WebClient会引发以下错误:


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)

1 个答案:

答案 0 :(得分:1)

我使用:

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

和:

  webClient = webClientBuilder
                .baseUrl(baseUrL)
                .filter(logRequest())
                .exchangeStrategies(ExchangeStrategies.builder().codecs(this::acceptedCodecs).build()).build();