springboot 1.4.0升级2.2.6版本后遇到的问题总结
SpringBoot版本从1.4.0升级为版本2.2.6
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!--<version>1.4.0.RELEASE</version>--> <version>2.2.6.RELEASE</version> </parent>
SpringCloud版本升级为对应匹配Hoxton.SR4
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<!--<version>Brixton.SR3</version>-->
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1、Sort排序时遇到的问题 has private access in 'org.springframework.data.domain.Sort'
原因:springboot2.2.1(含)以上的版本Sort已经不能再实例化了,构造方法已经是私有的了!
解决方法:改用Sort.by获得Sort对象
//旧版本 query.with(new Sort(Direction.DESC, "createTime")); //新版本 query.with(Sort.by(Direction.ASC, "createTime"));
2、不兼容的类型: com.mongodb.client.result.UpdateResult无法转换为com.mongodb.WriteResult
解决方法:WriteResult 替换 UpdateResult
3、对于BasicQuery(com.mongodb.BasicDBObject,com.mongodb.BasicDBObject), 找不到合适的构造器
原因: mongodb中QueryBuilder中DBObject被弃用,改为Document构造的解决方式
解决方法:Document 替换 BasicDBObject
//旧版本 Query query = new BasicQuery(new BasicDBObject(),new BasicDBObject()); //新版本 Query query = new BasicQuery(new Document(),new Document());
4、Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
原因:缺少因commons-pool2的依赖包
解决方法:添加下面依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.0</version> </dependency>
5、启动报错提示 nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/JedisPoolConfig
原因:缺少Jedis的依赖
解决方法:添加下面依赖
<!-- Spring Boot Redis依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <!-- 注意:1.4版本的依赖和2.0的依赖不一样,1.4名字里面没有“data”, 2.0必须是“spring-boot-starter-data-redis”--> <!-- <artifactId>spring-boot-starter-redis</artifactId> --> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 添加jedis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
6、org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration required a bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' that could not be found.
原因:缺少注解@EnableDiscoveryClient
@SpringBootApplication //版本升级后添加 @EnableDiscoveryClient @EnableEurekaClient @EnableCircuitBreaker @ComponentScan(basePackages = { "com.Demo.test" }) public class Application{}
7、springboot从1.3.5升级为2.2.6后@SpringApplicationConfiguration注解报错
原因:这个注解在1.4就被替换了,较新版的Spring Boot取消了@SpringApplicationConfiguration这个注解
解决方法:springboot1.4版本以及以后的版本,使用@SpringBootTest
8、Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
原因:提示信息表明数据库驱动com.mysql.jdbc.Driver'已经被弃用了、应当使用新的驱动com.mysql.cj.jdbc.Driver'
解決方法:
#修改前
#driverClassName: com.mysql.jdbc.Driver
#修改后
driverClassName: com.mysql.cj.jdbc.Driver
9、Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:
原因:swagger版本不匹配
解決方式:
<!-- swagger依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <!--<version>2.2.2</version>--> <!--修改前版本--> <version>2.6.1</version> <!--修改后版本--> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <!--<version>2.2.2</version>--> <!--修改前版本--> <version>2.6.1</version> <!--修改后版本--> </dependency>
10、部分依赖artifactid发生变化的。例如用到eureka、ribbon等依赖的需要将依赖artifactid改成新版本对应的名称。
例如
ribbon依赖修改
#修改前 <artifactId>spring-cloud-starter-ribbon</artifactId> #修改后 <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
eureka依赖修改
#修改前 <artifactId>spring-cloud-starter-eureka</artifactId> #修改后 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
hystrix依赖修改
<!-- hystrix依赖 --> <dependency> <groupId>org.springframework.cloud</groupId>
#修改前 <!--<artifactId>spring-cloud-starter-hystrix</artifactId>-->
#修改后 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
Spring Security oauth2依赖添加版本号
<dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> <version>1.0.10.RELEASE</version> </dependency>
10、Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
添加下面的依赖
<!-- ============Mybatis的 pageHelper ============ --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】