Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please

报错

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method modifyResponseBodyGatewayFilterFactory 
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.

分析

查看上面的描述信息,得到关键信息 ServerCodecConfigurer' that could not be found
因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web,两者会冲突。

查看GatewayAutoConfiguration源码

/**
 * @author Spencer Gibb
 * @author Ziemowit Stolarczyk
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "spring.cloud.gateway.enabled", matchIfMissing = true)
@EnableConfigurationProperties
@AutoConfigureBefore({ HttpHandlerAutoConfiguration.class,
		WebFluxAutoConfiguration.class })
@AutoConfigureAfter({ GatewayLoadBalancerClientAutoConfiguration.class,
		GatewayClassPathWarningAutoConfiguration.class })
@ConditionalOnClass(DispatcherHandler.class)
public class GatewayAutoConfiguration {
	@Bean
	public ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory(
			ServerCodecConfigurer codecConfigurer) {
		return new ModifyResponseBodyGatewayFilterFactory(codecConfigurer);
	}
}

接着查看gateway的配置类GatewayClassPathWarningAutoConfiguration

@Configuration(proxyBeanMethods = false)
	@ConditionalOnClass(name = "org.springframework.web.servlet.DispatcherServlet")
	protected static class SpringMvcFoundOnClasspathConfiguration {

		public SpringMvcFoundOnClasspathConfiguration() {
			log.warn(BORDER
					+ "Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. "
					+ "Please remove spring-boot-starter-web dependency." + BORDER);
		}

	}

上面也给出了提示,mvc项目和gateway不能兼容。
最后发现pom.xml中写了

        
        
            org.springframework.boot
            spring-boot-starter-web
        

spring-cloud-starter-gateway中用的是

    
      org.springframework.boot
      spring-boot-starter-webflux
      2.2.2.RELEASE
      compile
    
  •  

删除spring-boot-starter-web依赖

你可能感兴趣的:(Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please)