如何自定义SpringWebFlux WebClient xml + rss JAXB反序列化?

时间:2019-02-02 16:57:59

标签: java spring spring-boot jaxb spring-webflux

看一下Spring webflux客户端的json配置示例:

   ExchangeStrategies strategies = ExchangeStrategies
        .builder()
        .codecs(clientDefaultCodecsConfigurer -> {

        //how to do the same but xml?
        clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(new ObjectMapper(), MediaType.APPLICATION_JSON));
            clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(new ObjectMapper(), MediaType.APPLICATION_JSON));

        }).build();

WebClient webClient = WebClient.builder().exchangeStrategies(strategies).build();

this one相同的问题,只是针对jaxb xml序列化。 JAXB和xml是否有解码器实现?是否可以将HttpMessageConverter用作网络客户端的解码器,以使Spring Boot控制器和客户端实现保持一致?

1 个答案:

答案 0 :(得分:0)

我的猜测是Jaxb2XmlDecoder包中的Jaxb2XmlEncoderorg.springframework.http.codec.xml类:

ExchangeStrategies.builder()
        .codecs(configurer -> {
            configurer.defaultCodecs().jaxb2Decoder(new Jaxb2XmlDecoder());
            configurer.defaultCodecs().jaxb2Encoder(new Jaxb2XmlEncoder());
        })
        .build();