文章目录
- 项目启动报错1:`Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway`
- 项目启动报错2:`Failed to determine a suitable driver class`
- 项目启动报错3:`Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException`
- 总结
项目启动报错1:Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway
-
问题背景
由于我在公共模块中引入了
spring-boot-starter-web
依赖,后面在gateway模块中引入了公共模块,然后项目启动之时就报错Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway
-
问题原因
在类路径上找到了Spring MVC,与Spring Cloud Gateway不兼容。这是由于Spring Cloud Gateway和Spring MVC是两个不同的Web框架,它们具有不同的设计理念和工作方式。主要原因是它们之间存在以下几个不兼容的方面:
- 非阻塞与阻塞:Spring Cloud Gateway是一个反应式的网关,基于Reactor模型,使用异步非阻塞的方式处理请求,而Spring MVC是一个传统的阻塞式框架,使用同步阻塞的方式处理请求。
- 功能冲突:Spring MVC提供了完整的MVC功能,例如@Controller、@RequestMapping等注解,用于构建传统的Web应用程序。而Spring Cloud Gateway是专门用于构建微服务架构下的网关,主要关注路由、过滤、负载均衡等功能,没有提供传统MVC的各种注解和特性。
- 依赖冲突:Spring Cloud Gateway和Spring MVC都依赖于Spring Boot,但是它们在对应的依赖中使用了不同的starter。Spring Cloud Gateway使用spring-cloud-starter-gateway依赖,而Spring MVC使用spring-boot-starter-web依赖。这两个依赖中包含了不同版本的相关库,导致冲突。
为了解决这种不兼容性,需要根据实际需求进行调整。通过设置
spring.main.web-application-type=reactive
可以将应用程序标记为反应式应用程序,使其与Spring Cloud Gateway兼容。另外,如果不需要使用Spring MVC的功能,也可以直接删除spring-boot-starter-web
依赖项,从而避免冲突。 -
问题解决
排除公共模块中的
spring-boot-starter-web
依赖
项目启动报错2:Failed to determine a suitable driver class
之后项目再次启动报错Description:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
-
问题原因
我公共模块存储了实体类,实体类需要使用到MyBatisPlus依赖,所以我的公共模块中引入了MyBatisPlus依赖,而当前的gateway模块中也引入了公共模块,就顺带把MyBatisPlus依赖引入,MyBatisPlus依赖引入后如果启动SpringBoot程序,会自动去搜索链接数据库,检查配置文件是否有数据库连接配置,而我的Gateway模块鸭羹就没有使用数据库,所以这就导致报错
Failed to determine a suitable driver class
-
问题解决
-
方案一:通过
@SpringBootApplication
注解的exclude
属性排除数据库,从而告诉Spring我当前项目中并不会使用到数据库@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
-
方案二:排除公共模块中的MyBatisPlus依赖
解决方式和前面一样
-
项目启动报错3:Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
-
问题原因
由于我在公共模块中引入了Knife4j依赖,为了对DTO类中的字段使用
@ApiMode
进行API 文档描述,这就导致其它模块也简介引入了Knife4j,当前这个Knife4j在我的common模块和admin模块都是需要用到的,所以就放到了commom模块,但是我的interfas模块不需要,同时我的interfas模块还需要运行,由于当前项目是使用的 SpringBoot2.7 ,Knife4j是3.0.3,这里使用的Swagger版本是2.9.2
、springboot 版本是2。7.2
,发现是springboot版本太高,缺少swagger运行所需要的环境,具体缺少什么还没研究出来,所以只能回退到之前的版本,这在之前其实已经遇到过了说了一大堆,简而言之就是:SpringBoot和Swagger版本冲突,SpringBoot2.6开始就不在兼容Swagger3了
-
问题解决
-
方案一:改动版本,要么降低SpringBoot的版本,要么提高Swagger的版本(这里不建议这么做,改动版本风险太大了)
-
方案二:排除当前公共模块中的Knife4j(采用并成功解决)
-
方案三:让当前版本的SpringBoot能够兼容Swagger
spring: mvc: pathmatch: matching-strategy: ant_path_matcher # 支持 swagger3
-
总结
两处报错的原因都是由于依赖处理不当的原因,解决起来不难,提示都比较明显