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發生變化的。例如用到eurekaribbon等依賴的需要將依賴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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM