springboot2升級(jí)到springboot3過(guò)程相關(guān)修改記錄
近期項(xiàng)目被掃描出關(guān)于Spring Framework路徑遍歷漏洞(CVE-2024-38816),客戶(hù)要求整改,查了下springboot2需要升級(jí)到5.3.40才可以,但springboot2好像不太能很快就升級(jí),或者有可能不再會(huì)升級(jí)了,因此直接就將springboot升級(jí)到3了,本想著萬(wàn)年jdk8,但springboot3不支持jdk8,看樣子要打破了,因此利用周末做了一下升級(jí)測(cè)試,記錄如下:
以下為我進(jìn)行springboot2升級(jí)到springboot3過(guò)程相關(guān)修改記錄,備查,你的項(xiàng)目不一定用到下面所有的,可以參考著改
主要修改
jdk升級(jí)
按springboot3要求,升級(jí)到j(luò)dk17或jdk21,我這邊是升級(jí)到j(luò)dk17
spring-boot-starter-parent
spring-boot-starter-parent依賴(lài)版本升級(jí)
升級(jí)前
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> <relativePath /> <!-- lookup parent from repository --> </parent>
升級(jí)后
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.0</version> <relativePath /> <!-- lookup parent from repository --> </parent>
jdk源碼編碼修改,將1.8改成17或21
修改前
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
修改后
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build>
javax.servlet.*相關(guān)的類(lèi)找不到,需要切換依賴(lài)為jakarta.servlet
<!--jakarta.servlet start --> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> </dependency> <!--jakarta.servlet end -->
同時(shí),將所有javax.servlet.*修改為jakarta.servlet.*
另外校驗(yàn)類(lèi)相關(guān)的也進(jìn)行修改javax.validation.*修改為jakarta.validation.*
mybatis-plus-boot-starter升級(jí)
mybatis-plus-boot-starter需要升級(jí),不升可能會(huì)報(bào)Invalid value type for attribute ‘factoryBeanObjectType’: java.lang.String,需要升級(jí)一下依賴(lài)
升級(jí)成:
mybatis-plus-spring-boot3-starter
,我使用的是3.5.5
<!-- mybatis-plus start--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.5</version> </dependency> <!-- mybatis-plus end-->
redis修改
配置上需要加上data
需要修改redis的配置:由原來(lái)的spring.redis.修改為spring.data.redis.
改完后重啟又發(fā)現(xiàn)如下錯(cuò)誤:
Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not “opens java.lang” to unnamed module @2bbaf4f0
原因是redission版本比較低,我升級(jí)版本后就沒(méi)問(wèn)題了
升級(jí)前版本:<redisson.version>3.12.5</redisson.version>
升級(jí)后版本:<redisson.version>3.40.2</redisson.version>
swagger升級(jí)到springboot3
<swagger3.version>2.7.0</swagger3.version>
<!-- swagger3 start --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>${swagger3.version}</version> </dependency> <!-- swagger3 end -->
swagger3默認(rèn)是開(kāi)啟了接口和doc的訪問(wèn)的如引入上面的依賴(lài)后,可以通過(guò)如下地址訪問(wèn)(假如端口為8080,context-path為/test)
http://localhost:8080/test/swagger-ui/index.html http://localhost:8080/test/v3/api-docs
注:如果生產(chǎn)環(huán)境中需要禁用,使用如下配置進(jìn)行,分別進(jìn)行ui的禁用和api-docs的禁用
springdoc: swagger-ui: enabled: true api-docs: enabled: true
其他修改參考
如果用到了阿里巴巴druid數(shù)據(jù)源,最好升級(jí)到新版本
我這里是從druid的1.2.12版本升級(jí)到了1.2.24
注:我使用的是編程式的阿里巴巴的監(jiān)控配置,WebStatFilter和StatViewServlet的實(shí)現(xiàn)類(lèi)需要修改,如果不是用編程式,可以不用管這一步
import com.alibaba.druid.support.jakarta.WebStatFilter; import jakarta.servlet.annotation.WebFilter; import jakarta.servlet.annotation.WebInitParam; //注意不要忘記在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 注解,不然就是404了。 @WebFilter( filterName = "druidWebStatFilter", urlPatterns = "/*", initParams = { @WebInitParam(name = "exclusions", value = "weburi.json,.html,.js,.gif,.jpg,.png,.css,.ico,/druid/*") // 忽略資源 }) public class DruidStatFilter extends WebStatFilter { }
import com.alibaba.druid.support.jakarta.StatViewServlet; import jakarta.servlet.annotation.WebInitParam; import jakarta.servlet.annotation.WebServlet; //注意不要忘記在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 注解,不然就是404了。 @WebServlet(urlPatterns="/druid/*", initParams={ @WebInitParam(name="allow",value=""),// IP白名單(沒(méi)有配置或者為空,則允許所有訪問(wèn)) @WebInitParam(name="deny",value=""),// IP黑名單 (存在共同時(shí),deny優(yōu)先于allow) @WebInitParam(name="loginUsername",value="admin"),// 用戶(hù)名 @WebInitParam(name="loginPassword",value="123456"),// 密碼 @WebInitParam(name="resetEnable",value="true")// 啟用HTML頁(yè)面上的“Reset All”功能 }) public class DruidStatViewServlet extends StatViewServlet { private static final long serialVersionUID = -2688872071445249539L; }
LocalVariableTableParameterNameDiscoverer類(lèi)找不到
springboot3中沒(méi)有這個(gè)類(lèi)了,需要修改成org.springframework.core.StandardReflectionParameterNameDiscoverer
到此這篇關(guān)于springboot2升級(jí)到springboot3過(guò)程相關(guān)修改的文章就介紹到這了,更多相關(guān)springboot2升級(jí)到springboot3內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接池的方法
這篇文章主要介紹了Java實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接池的方法,涉及java數(shù)據(jù)庫(kù)連接池的創(chuàng)建、連接、刷新、關(guān)閉及狀態(tài)獲取的常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07SpringBoot整合Elasticsearch實(shí)現(xiàn)索引和文檔的操作方法
Elasticsearch 基于 Apache Lucene 構(gòu)建,采用 Java 編寫(xiě),并使用 Lucene 構(gòu)建索引、提供搜索功能,本文分步驟通過(guò)綜合案例給大家分享SpringBoot整合Elasticsearch的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2021-05-05

Springboot整合GuavaCache緩存過(guò)程解析

springBoot 插件工具熱部署 Devtools的步驟詳解

Spring Data JPA中的動(dòng)態(tài)查詢(xún)實(shí)例