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

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