springboot2升級(jí)到springboot3過(guò)程相關(guān)修改記錄
近期項(xiàng)目被掃描出關(guān)于Spring Framework路徑遍歷漏洞(CVE-2024-38816),客戶要求整改,查了下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"),// 用戶名 @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 springboot設(shè)計(jì)實(shí)現(xiàn)的圖書(shū)管理系統(tǒng)(建議收藏)
這篇文章主要介紹了使用java springboot設(shè)計(jì)實(shí)現(xiàn)的圖書(shū)管理系統(tǒng),包含了整個(gè)的開(kāi)發(fā)過(guò)程,以及過(guò)程中遇到的問(wèn)題和解決方法,對(duì)大家的學(xué)習(xí)和工作具有借鑒意義,建議收藏一下2021-08-08基于Java實(shí)現(xiàn)簡(jiǎn)易的七星彩號(hào)碼生成器
七星彩是中國(guó)體育彩票的一種玩法,由中國(guó)國(guó)家體育總局體育彩票管理中心統(tǒng)一發(fā)行。本文為大家準(zhǔn)備了一個(gè)七星彩號(hào)碼生成器Java工具類(lèi),感興趣的可以了解一下2022-08-08springboot中nacos-client獲取配置的實(shí)現(xiàn)方法
本文主要介紹了springboot中nacos-client獲取配置的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04springboot整合mybatis將sql打印到日志的實(shí)例詳解
這篇文章主要介紹了springboot整合mybatis將sql打印到日志的實(shí)例詳解,需要的朋友可以參考下2017-12-12