@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;
}
}
public SpringDecoder(ObjectFactory<HttpMessageConverters> messageConverters)
已弃用.你应该使用另一个构造函数:
public SpringDecoder(ObjectFactory<HttpMessageConverters> messageConverters,
ObjectProvider<HttpMessageConverterCustomizer> customizers)
例如:
@Bean
public Decoder feignDecoder(ObjectProvider<HttpMessageConverterCustomizer> customizers) {
// HttpMessageConverter<?> jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
// HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jacksonConverter);
var httpMessageConverters = new HttpMessageConverters();
return new ResponseEntityDecoder(new SpringDecoder(() -> httpMessageConverters, customizers));
}
@Bean
public Encoder feignEncoder() {
var httpMessageConverters = new HttpMessageConverters();
return new SpringEncoder(() -> httpMessageConverters);
}
如果你想写一个解码时间定制器,那么你可以这样写:
@Component
public class HttpMessageCustomizer implements HttpMessageConverterCustomizer {
@Override
public void accept(List<HttpMessageConverter<?>> httpMessageConverters) {
//customizing
}
@Override
public Consumer<List<HttpMessageConverter<?>>> andThen(Consumer<? super List<HttpMessageConverter<?>>> after) {
return HttpMessageConverterCustomizer.super.andThen(after);
}
}
5条答案
按热度按时间pbwdgjma1#
根据文档,您可以为您的Feign客户端提供自定义解码器,如下所示。
虚拟客户端接口:
伪装客户端自定义配置:
mbzjlibv2#
按照@NewBie的回答,我可以给你更好的给予.
如果你想在feign客户端中使用Jackson消息转换器,请使用JacksonDecoder,因为SpringDecoder会增加feign客户端调用平均延迟在生产环境中。
1dkrff033#
@NewBie的回答存在严重的性能问题,在
new HttpMessageConverters
进程中会执行loadclass,导致大量线程阻塞,如果你用过这段代码,请修改如下:改变
你可以使用JMeter和Arthas来重现这种现象,修改后的程序有了很大的改进。
8oomwypt4#
如下定义一个自定义解码器,使用
@Configuration
进行注解,并设置为feign客户端接口configuration = CustomFeignClientConfig.class
的参数jucafojl5#
你可以使用SpringDecoder和SpringEncoder。
已弃用.你应该使用另一个构造函数:
例如:
如果你想写一个解码时间定制器,那么你可以这样写: