2

我已设法建立与沙盒服务器的 SSL 连接,并通过应用内容类型 MediaType.APPLICATION_XML 将对象作为序列化 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)
4

1 回答 1

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();
于 2020-02-09T18:15:41.550 回答