当用到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.atguigu.gulimall</groupId> <artifactId>gulimall-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
解决的方法:
在gateway项目pom依赖中的自定义模块依赖使用标签<exclusion>去除spring-boot-starter-web依赖。
<!--自定义依赖-->
<dependency>
<groupId>com.atguigu.gulimall</groupId><artifactId>gulimall-common</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
spring-boot-starter-web 是WEB开发的必备依赖,Web开发就是开发网页程序,从开发简单的纯文本静态页面到复杂的基于Web的互联网应用程序,电子商务和社交网络服务。开发人员不需要从零开始写代码,可通过调用框架来开发程序,以节省时间和成本。
spring-boot-starter-web 的作用是在Spring Boot的基础上简化Web应用程序的配置和部署过程。它包含了一系列与Web应用程序开发相关的依赖项,如Spring Web MVC框架、Tomcat服务器以及其他必要的库。通过引入这个依赖启动器,开发者可以快速搭建一个基于Spring Boot的Web应用程序,而无需手动配置和部署Web服务器和应用程序容器。此外,它还整合了Spring MVC所需的核心组件,如spring-aop、spring-beans、spring-context、spring-core、spring-web和spring-webmvc,利用Maven的父子依赖关系实现,使得整个Web开发场景的配置更加简洁和高效。