Spring Gateway集成OpenFeign启动失败 缺少HttpMessageConverters Bean
Spring Cloud Gateway集成OpenFeign启动报HttpMessageConverters缺失问题
问题现象
使用Spring Boot 2.7.0搭建Spring Cloud API网关,原有逻辑运行正常,将RestTemplate替换为OpenFeign后启动失败。
环境依赖配置
build.gradle核心配置如下:
plugins { id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' id 'org.liquibase.gradle' version '2.1.0' id 'groovy' } ext { set('springCloudVersion', "2021.0.3") } // 已注释Servlet栈web依赖,使用WebFlux栈 // implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' implementation 'org.springframework.cloud:spring-cloud-starter-gateway' implementation 'org.springframework.cloud:spring-cloud-starter-config' implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
启动类添加了对应开启注解:
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ApiGatewayApplication { // 启动类核心逻辑 }
编写的Feign客户端代码如下:
@FeignClient(name="identity-service") public interface IdentityServiceClient { @GetMapping("/api/all") public List<ApiKey> getAllApiKeys(); }
报错信息
启动时抛出核心错误如下:
o.s.c.openfeign.FeignClientFactoryBean : For 'identity-service' URL not provided. Will try picking an instance via load-balancing. DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate .... onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiGatewayApplication': Invocation of init method failed; nested exception is 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)} com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ... com.netflix.discovery.DiscoveryClient : Unregistering ... com.netflix.discovery.DiscoveryClient : DiscoveryClient_API-GATEWAY/192.168.1.72:api-gateway:8081 - deregister status: 404 com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient *************************** APPLICATION FAILED TO START *************************** Description: A component required a bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' that could not be found. Action: Consider defining a bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' in your configuration.
初步排查特征
- 公开示例文档均未提及需要手动配置
HttpMessageConverters,该组件按自动装配逻辑应随spring-web、spring-boot-starter-webflux依赖加载。 - 不通过
@Autowired注入Feign客户端、业务服务时,应用可正常启动;一旦使用@Autowired注入上述组件,就会复现报错。
根因分析
- Spring Cloud Gateway基于WebFlux响应式栈构建,默认不会触发Servlet栈相关的自动配置,而
HttpMessageConverters的自动配置类是绑定Spring MVC(Servlet栈)场景的,纯WebFlux环境下默认不会创建该Bean。 - OpenFeign默认的编码器、解码器实现强依赖
HttpMessageConverters完成请求/响应的序列化反序列化。Feign客户端默认是懒初始化的:如果代码中没有注入Feign客户端,就不会触发Feign实例的初始化流程,不会校验依赖的Bean;一旦通过@Autowired注入Feign客户端,就会触发Feign实例初始化,加载默认解码器时找不到HttpMessageConvertersBean,直接抛出启动错误。 - 注意:不要为了获取这个Bean引入
spring-boot-starter-web依赖,Servlet栈的web依赖会和WebFlux栈的Gateway产生冲突,导致网关以Servlet容器模式启动,网关路由、过滤器等核心功能会失效。
解决方案
在配置类中手动注册HttpMessageConverters Bean即可,不需要引入额外的Servlet栈依赖:
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.beans.factory.ObjectProvider; import org.springframework.http.converter.HttpMessageConverter; @Configuration public class FeignConfig { @Bean public HttpMessageConverters httpMessageConverters(ObjectProvider<HttpMessageConverter<?>> converters) { return new HttpMessageConverters(converters.orderedStream().toList()); } }
添加该配置后重启应用,Feign客户端即可正常注入、调用。如果需要完全适配响应式场景,也可以替换使用Spring Cloud官方提供的响应式Feign组件,适配WebFlux栈的编解码逻辑,不需要额外注册上述Bean。
内容的提问来源于stack exchange,提问作者sonoerin
相关产品推荐
相关产品推荐

