Spring Cloud Gateway 启动报错(因为web依赖)
Spring Cloud Gateway 启动报错(因为web依赖)
报错信息:
Spring Cloud Gateway启动一直报错 详细错误信息
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.
查看源码:
1、GatewayClassPathWarningAutoConfiguration.java
spring cloud Gateway网关本身是一个Spring Boot项目,在网关微服务启动时他会自动加载它的配置,其中其中有一个叫做GatewayClassPathWarningAutoConfiguration的配置类, 如下:
看到@ConditionalOnMissingClass({"org.springframework.web.reactive.DispatcherHandler"})
在没有DispatcherHandler的情况下提示添加 spring-boot-starter-webflux 依赖。
在有DispatcherHandler的情况下提示移除 spring-boot-starter-web 依赖。
@Configuration(
proxyBeanMethods = false
)
@AutoConfigureBefore({GatewayAutoConfiguration.class})
public class GatewayClassPathWarningAutoConfiguration {
private static final Log log = LogFactory.getLog(GatewayClassPathWarningAutoConfiguration.class);
private static final String BORDER = "\n\n**********************************************************\n\n";
public GatewayClassPathWarningAutoConfiguration() {
}
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnMissingClass({"org.springframework.web.reactive.DispatcherHandler"}) //重点注解
protected static class WebfluxMissingFromClasspathConfiguration {
public WebfluxMissingFromClasspathConfiguration() {
GatewayClassPathWarningAutoConfiguration.log.warn("\n\n**********************************************************\n\nSpring Webflux is missing from the classpath, which is required for Spring Cloud Gateway at this time. Please add spring-boot-starter-webflux dependency.\n\n**********************************************************\n\n");
}
}
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass(
name = {"org.springframework.web.servlet.DispatcherServlet"}
)
protected static class SpringMvcFoundOnClasspathConfiguration {
public SpringMvcFoundOnClasspathConfiguration() {
GatewayClassPathWarningAutoConfiguration.log.warn("\n\n**********************************************************\n\nSpring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.\n\n**********************************************************\n\n");
}
}
}
如果pom文件中添加了spring-boot-starter-web 依赖,项目中就会存在 org.springframework.web.servlet.DispatcherServlet过滤器,这样与Spring Cloud Gateway冲突不兼容。
2、GatewayAutoConfiguration.java
打开网关自动配置类中GatewayAutoConfiguration 这个类,看到如下几个方法中需要org.springframework.http.codec.ServerCodecConfigurer这个bean
@Bean
public ModifyRequestBodyGatewayFilterFactory modifyRequestBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer) {
return new ModifyRequestBodyGatewayFilterFactory(codecConfigurer.getReaders());
}
@Bean
public DedupeResponseHeaderGatewayFilterFactory dedupeResponseHeaderGatewayFilterFactory() {
return new DedupeResponseHeaderGatewayFilterFactory();
}
@Bean
public ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer, Set<MessageBodyDecoder> bodyDecoders, Set<MessageBodyEncoder> bodyEncoders) {
return new ModifyResponseBodyGatewayFilterFactory(codecConfigurer.getReaders(), bodyDecoders, bodyEncoders);
}
解决办法:
spring cloud gateway是基于webflux的,所以直接移除spring-boot-start-web依赖,如果需要web服务,添加spring-boot-starter-webflux依赖。
posted on 2022-02-28 13:50 Chase_Hanky 阅读(3929) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了