gpt4 book ai didi

spring - 如何使用 Spring Cloud Netflix Feign 设置自定义 Jackson ObjectMapper

转载 作者:行者123 更新时间:2023-12-02 20:21:23 25 4
gpt4 key购买 nike

我遇到了一种情况,我需要为第三方 API 定义一次性 @FeignClient。在此客户端中,我想使用与我的 @Primary 不同的自定义 Jackson ObjectMapper。我知道可以覆盖 spring 的 feign 配置默认值,但是我不清楚如何仅通过此特定客户端来简单地覆盖 ObjectMapper。

最佳答案

根据 documentation ,您可以为您的 Feign 客户端提供自定义解码器,如下所示。

假客户端接口(interface):

@FeignClient(value = "foo", configuration = FooClientConfig.class)
public interface FooClient{
//Your mappings
}

Feign 客户端自定义配置:

@Configuration
public class FooClientConfig {

@Bean
public Decoder feignDecoder() {
HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());

HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jacksonConverter);
ObjectFactory<HttpMessageConverters> objectFactory = () -> httpMessageConverters;


return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}

public ObjectMapper customObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
//Customize as much as you want
return objectMapper;
}
}

关于spring - 如何使用 Spring Cloud Netflix Feign 设置自定义 Jackson ObjectMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35853908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com