项目场景:
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
很好,这显然是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的方式都能解决问题,为了避免缺少些什么,还是按照配置类源码的配置方法去配一次吧