How to customize SpringWebFlux WebClient JSON deserialization?

后端 未结 6 499
我在风中等你
我在风中等你 2020-12-06 04:15

I\'m using a spring-webflux WebClient (build 20170502.221452-172) to access a Web application producing a stream of Entry objects (application/stream+json) like th

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 04:52

    Based on the replies above, I ended up with this code:

    final ObjectMapper mapper = new ObjectMapper()
        .findAndRegisterModules()
        .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    final ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder()
        .codecs(configurer -> configurer.defaultCodecs()
        .jackson2JsonDecoder(new Jackson2JsonDecoder(mapper)))
        .build();
    final WebClient webClient = WebClient.builder()
        .exchangeStrategies(exchangeStrategies)
        .build();
    

    If you don't include .findAndRegisterModules(), you're going to have problems when you want to deserialise things like Java 8's time objects.

提交回复
热议问题