springboot2升級到springboot3過程相關(guān)修改記錄
近期項目被掃描出關(guān)于Spring Framework路徑遍歷漏洞(CVE-2024-38816),客戶要求整改,查了下springboot2需要升級到5.3.40才可以,但springboot2好像不太能很快就升級,或者有可能不再會升級了,因此直接就將springboot升級到3了,本想著萬年jdk8,但springboot3不支持jdk8,看樣子要打破了,因此利用周末做了一下升級測試,記錄如下:
以下為我進(jìn)行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)的也進(jì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默認(rèn)是開啟了接口和doc的訪問的如引入上面的依賴后,可以通過如下地址訪問(假如端口為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ù)源,最好升級到新版本
我這里是從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 springboot設(shè)計實現(xiàn)的圖書管理系統(tǒng)(建議收藏)
這篇文章主要介紹了使用java springboot設(shè)計實現(xiàn)的圖書管理系統(tǒng),包含了整個的開發(fā)過程,以及過程中遇到的問題和解決方法,對大家的學(xué)習(xí)和工作具有借鑒意義,建議收藏一下2021-08-08
springboot中nacos-client獲取配置的實現(xiàn)方法
本文主要介紹了springboot中nacos-client獲取配置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
springboot整合mybatis將sql打印到日志的實例詳解
這篇文章主要介紹了springboot整合mybatis將sql打印到日志的實例詳解,需要的朋友可以參考下2017-12-12

