spring cloud gateway启动报错:org.springframework.cloud.gateway.config.GatewayAutoConfiguration

springcloud 启动一直报错 详细错误信息 

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.

解决办法

将pom文件中的spring-boot-starter-web 移除

 

详细分析

点进去 GatewayAutoConfiguration 这个类

@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 bodyDecoders, Set bodyEncoders) {
    return new ModifyResponseBodyGatewayFilterFactory(codecConfigurer.getReaders(), bodyDecoders, bodyEncoders);
}

 

确实需要这个bean 

spring cloud gateway server项目是一个spring boot项目,在启动的时候会去加载它的配置,其中有一个叫做GatewayClassPathWarningAutoConfiguration的配置类 代码入下

@Configuration(
    proxyBeanMethods = false
)
//在 GatewayAutoConfiguration 执行执行 
@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不兼容

spring cloud gateway是基于webflux的,如果需要web支持的话需要导入spring-boot-starter-webflux取代spring-boot-start-web。

 

你可能感兴趣的:(springcloud,spring-cloud,gateway启动报错)