Spring Cloud gateway启动报错:spring
2021-04-16 本文已影响0人
蓉漂里的小白
今天在集成spring cloud 和 gateway时候报了一个错
spring mvc found on classpath, which is incompatible with spring cloud gateway
这样一个错误,经过网上和官网查找原因是因为spring cloud gateway 是建立在spring boot 2.x 和 spring webflux基础上的既:gateway 本身已经包含了spring mvc 的功能,正与提示的一样和spring boot 的web starter冲突了
官网原文(https://docs.spring.io/spring-cloud-gateway/docs/2.2.7.RELEASE/reference/html/)
Spring Cloud Gateway is built on [Spring Boot 2.x](https://spring.io/projects/spring-boot#learn), [Spring WebFlux](https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html), and [Project Reactor](https://projectreactor.io/docs). |
解决办法也很简单就是把spring boot 的web starter 去掉就OK了。
我的项目结构是:parent_pom 引入了web-starter 然后在maven 子项目中引入了gateway
1: 我的解决方案
所以我直接把parent pom 中的web starter去掉就OK了
父pom中注释掉web starter:
<!-- Spring Boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>-->
Maven 子项目的pom直接引入gateway starter:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
最后看一下配置
spring:
cloud:
gateway:
routes:
- id: gateway
uri: http://www.bing.com
predicates:
- Path=/user/**
filters:
- StripPrefix=2
当我们在浏览器输入
http://localhost:8090/user/x 就会自动跳转到 https://www.bing.com/
2: 网上有很多博客都是这样写的:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</exclusion>
</exclusions>
</dependency>
这样会出现一个问题就是,对与maven子项目并且只引入了spring-cloud-starter-gateway, 这样的操作会导致你启动的时候提示
你找不到spring boot application相关的注解,因为你已经把spring-boot-starter-web jar包移除了,并不能解决这个问题。