当用到gateway网关时,启动GatewayWebApplication报如下错误:
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
第一种情况:如果没有用公共模块来存放公共依赖和代码方法如下:
解决的方法:
1.在gateway模块中: gateway依赖中 将gateway中的“spring-boot-starter-webflux” 去掉可以编译成功 ,可以编译成功
2.在gateway项目pom依赖中使用标签<exclusion>去除spring-boot-starter-web依赖。
<!--引入gateway 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<!--排除gateway 内部不兼容的 spring-webflux -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</exclusion>
<!-- 排除web依赖-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
第二种情况:就是项目中用了公共模块的依如下图,
<dependency> <groupId>com.chaoshi</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version></dependency>
解决的方法:
1. 在gateway项目pom依赖中的自定义模块依赖使用标签<exclusion>去除spring-boot-starter-web依赖。
<!--自定义依赖-->
<dependency>
<groupId>com.chaoshi</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
2.问题解决!!!!