【问题标题】:After update do spring-boot 2.3.0 webflux webclient ignores added jackson2JsonDecoder更新后做 spring-boot 2.3.0 webflux webclient 忽略添加的 jackson2JsonDecoder
【发布时间】:2020-10-14 20:36:56
【问题描述】:

在我更新到 spring-boot 2.3.0 之后(所以我的 webflux WebClient 现在是 5.2.6 版)自定义 ObjectMapper 被完全忽略。我的配置如下所示

public static WebClient buildWebClient(WebClient.Builder builder) {
    return builder
            .defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
            .defaultHeader(HttpHeaders.ACCEPT_ENCODING, "identity")
            .exchangeStrategies(ExchangeStrategies
                    .builder()
                    .codecs(codecConfigurer -> {
                        codecConfigurer.defaultCodecs().jackson2JsonDecoder(buildJsonDeserializer());
                    })
                    .build())
            .build();
} 

public static Jackson2JsonDecoder buildJsonDeserializer() {
    ObjectMapper customObjectMapper = new ObjectMapper();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.addDeserializer(Map.class, new JsonDeserializer<Map<String, String>>() {
        @Override
        public Map<String, String> deserialize(JsonParser p, DeserializationContext ctxt)
                throws IOException {
            ...
        }
    });
    customObjectMapper.registerModule(simpleModule);
    return new Jackson2JsonDecoder(customObjectMapper, MediaType.APPLICATION_JSON);
}

新版本有bug还是只是改了不知道怎么设置?

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    WebClient.builder()
            .defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
            .defaultHeader(HttpHeaders.ACCEPT_ENCODING, "identity")
            .codecs(configurer -> {
              configurer.customCodecs().registerWithDefaultConfig(buildJsonDeserializer());
            })
            .build();
    

    这是官方文档的link

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2021-04-19
      • 2018-05-09
      • 2019-03-15
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      相关资源
      最近更新 更多