针对feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found

第一次写博客,以前都是在csdn上面看各位大神的经验解决问题
针对我遇到的远程调用 feign的问题:feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.xxx.xxxx.xxxx.xxx.xxx] and content type [text/html;charset=utf-8]

查了ai说要在FeignClient接口的方法加produces = ...

@GetMapping(value = "/api/order/getOrderInfoByOrderNo/{orderNo}",produces = "application/json")

要不就是在api类的GetMapping上加produces = "application/json" 均无果

又或者是加一个配置类:

@Configuration
public class MyFeignConfig {

    private final ObjectFactory<HttpMessageConverters> messageConverters;

    public MyFeignConfig(ObjectFactory<HttpMessageConverters> messageConverters) {
        this.messageConverters = messageConverters;
    }

    @Bean
    public Request.Options requestOptions() {
        return new Request.Options(5000, 10000); // 连接超时和读取超时分别为5秒和10秒
    }

    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(100, 1000, 3); // 初始间隔100ms,最大间隔1秒,重试次数3次
    }

}

加配置类要在你的FeignClient上引一下,configuration = My...ion.class,还是不行

最后,好兄弟丢给我一个配置类,在你的FeignClient接口的父包或者爷爷包 可能是个client,和这个爷爷包加一个同级的包config,里面加上这个配置类:


@Slf4j
@Configuration
public class FeignConfig {
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 1.使用RequestContextHolder拿到刚进来的请求数据
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

                if (requestAttributes != null) {
                    // 老请求
                    HttpServletRequest request = requestAttributes.getRequest();
                    Enumeration<String> headerNames = request.getHeaderNames();
                    if (headerNames != null) {
                        // 2.同步请求头的数据
                        while (headerNames.hasMoreElements()) {
                            String name = headerNames.nextElement();
                            String values = request.getHeader(name);
                            if("content-type".equals(name)) {
                                continue;
                            }
                            template.header(name, values);
                        }
                    }
                    log.warn("请求token:{}", request.getHeader("token"));
                }
            }
        };
    }
}

问题解决,好兄弟牛逼!

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值