更新后做 spring-boot 2.3.0 webflux webclient 忽略添加的 jackson2JsonDecoder
After update do spring-boot 2.3.0 webflux webclient ignores added jackson2JsonDecoder
在我更新到 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或者只是改了一些东西现在不知道怎么设置?
试试这个:
WebClient.builder()
.defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT_ENCODING, "identity")
.codecs(configurer -> {
configurer.customCodecs().registerWithDefaultConfig(buildJsonDeserializer());
})
.build();
这里是link官方文档。
在我更新到 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或者只是改了一些东西现在不知道怎么设置?
试试这个:
WebClient.builder()
.defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT_ENCODING, "identity")
.codecs(configurer -> {
configurer.customCodecs().registerWithDefaultConfig(buildJsonDeserializer());
})
.build();
这里是link官方文档。