springcloud gateway网关调用其他微服务报feign.codec.DecodeException 异常

项目场景:

springcloud gateway网关调用其他微服务报feign.codec.DecodeException 异常

feign.codec.DecodeException: No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 at feign.AsyncResponseHandler.decode(AsyncResponseHandler.java:119)
 at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:87)
 at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)
 at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
 at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100)
 at com.sun.proxy.$Proxy109.getToken(Unknown Source)
 at item.inkcloud.gateway.GatewayApplicationTests.contextLoads(GatewayApplicationTests.java:31)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:498)
 at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
 at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
 at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
 at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
 at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
 at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
 at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
 at org.junit.jupiter.engine.execution.InvocationInterceptorChain$Intercep

原因分析:

我们可以看到报错信息,我们抓住重点
No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessageConverters’ available
我们可以知道IOC容器中缺失了httpmessageconverters这个bean

我使用的是idea ,使用 ctrl + N 打开搜索框 我们搜索httpmessageconverters 可以看到搜索结果中有HttpMessageConvertersAutoConfiguration

很好,这显然是Springboot进行相关自动配置的类,我们点进去看看

果不其然,我们在自动配置类中找到了HttpMessageConverters这个bean的装配

@Bean
@ConditionalOnMissingBean
public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
    return new HttpMessageConverters((Collection)converters.orderedStream().collect(Collectors.toList()));
}

暂时不知道bean缺少的原因是什么,抱着实用主义,先把问题解决了再说
既然缺少了bean,那我自己再往容器配一个就是了

解决方案:

@SpringBootConfiguration
public class HttpMsgConverterConfig {


    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {

        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }


//    @Bean
//    public HttpMessageConverters httpMessageConverters(){
//        return new HttpMessageConverters();
//    }
//    
    
}

经过实践,2种配bean的方式都能解决问题,为了避免缺少些什么,还是按照配置类源码的配置方法去配一次吧

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值