当前位置: 首页 > ops >正文

springboot2.x升级springboot3.x

springboot2.x升级springboot3.x

  • 背景
    • 升级jdk版本为17以上
    • springboot版本修改
    • javax包更新
    • mybatis-plus升级
    • swagger升级springdoc
    • springdoc配置

背景

当前项目是springboot2.5.9版本的springboot+mybatis-plus项目,需要升级到springboot3.5.0项目。

升级jdk版本为17以上

Spring Boot 3.x 需 ​Java 17+​,检查并升级 JDK:

<properties><java.version>17</java.version>
</properties>

springboot版本修改

修改 pom.xml 中的父依赖版本:

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.9</version><relativePath/> <!-- lookup parent from repository --></parent>

升级版本

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.0</version><relativePath/> <!-- lookup parent from repository --></parent>

javax包更新

全局替换 javax.* 为 jakarta.*(涉及 Servlet、JPA 等):

// 旧:import javax.servlet.HttpServletRequest;
// 新:import jakarta.servlet.HttpServletRequest;

在这里插入图片描述

mybatis-plus升级

 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.12</version></dependency><!--mybatis-plus分页插件使用必须依赖项--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>3.5.12</version></dependency>

这里面分页需要注意,必须引入新的依赖。

swagger升级springdoc

Spring 社区已明确推荐使用 ​springdoc-openapi​ 替代 springfox,因其原生支持 Jakarta EE 并与 Spring Boot 3.x 完全兼容

老版本的swagger

<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency>

替换为新的swagger

 <dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.8.8</version></dependency>

springdoc配置

调整路径匹配策略

Spring Boot 3.x 默认使用 PathPatternParser,但 Swagger UI 需切换为传统 AntPathMatcher:
application.yml

spring:mvc:pathmatch:matching-strategy: ant_path_matcher

删除swaggerconfig配置类

新增springdoc配置类


@Configuration
@SecurityScheme(name = "Bearer Authentication",type = SecuritySchemeType.HTTP,bearerFormat = "JWT",scheme = "bearer"
)
public class SpringDocConfig {@Beanpublic OpenAPI customOpenApi() {return new OpenAPI().info(new Info().title("xxxAPI 文档").description("这是一个xxx系统的API文档。").version("1.0").contact(new Contact().name("开发团队").email("support@example.com").url("https://github.com/")).license(new License().name("MIT License").url("https://opensource.org/licenses/MIT")).termsOfService("http://example.com/terms/")).externalDocs(new ExternalDocumentation().description("更多文档").url("http://example.com/docs")).addSecurityItem(new SecurityRequirement().addList("Bearer Authentication"));}@Beanpublic GroupedOpenApi allApi() {return GroupedOpenApi.builder().group("所有接口").pathsToMatch("/**").packagesToScan("com.xxx.xxx.controller").build();}

spring-doc配置

springdoc:api-docs:enabled: truepath: /v3/api-docsswagger-ui:path: /swagger-ui.htmlurl: /v3/api-docsdisable-swagger-default-url: falsetags-sorter: alphaoperations-sorter: alphapackages-to-scan: com.xxx.controllerpaths-to-match: /**show-actuator: falsedefault-produces-media-type: application/jsondefault-consumes-media-type: application/json

其他修改:

依次将swagger的注解更改为springdoc的注解
主要使用springdoc的@Schema注解对类和属性进行标注:@Schema(description = "xxx对象")
http://www.xdnf.cn/news/12683.html

相关文章:

  • 端午编程小游戏--艾草驱邪
  • 【SpringBoot自动化部署方法】
  • UDP与TCP通信协议技术解析
  • XXL-JOB——源码分析解读(1)
  • 英语词汇表格 form, table, sheet, grid, tabulation 的区别
  • 打卡第38天:早停策略和模型权重的保存
  • Maven的配置与运行
  • 【强化学习】——04Model-Based RL
  • 垃圾回收相关八股
  • IDEA中的debug使用技巧
  • 伽罗华域GF(galois field)的乘法计算 - 查表法
  • Three.js实现梦幻星光漩涡特效 - 从原理到实现
  • Python 基础核心语法:输入输出、变量、注释与字符串操作
  • FirmAE安装-重新写
  • JDK17安装与配置
  • 心理咨询技能竞赛流程方案
  • Python Day45
  • 业余无线电FT8信道调制之LDPC编码
  • EMD算法
  • 复变函数极限介绍与MATLAB演示
  • 【仿生机器人】仿生机器人智能架构:从感知到个性的完整设计
  • 从零开始的python学习(七)P102+P103+P104+P105+P106+P107
  • Python 中的上下文管理器:使用 with 关键字高效管理资源
  • 【Redis系列 04】Redis高可用架构实战:主从复制与哨兵模式从零到生产
  • 第10篇《数据库中间件集成监控与全链路观测系统设计》
  • 2007-2023年数字经济上市公司专利申请获得数据
  • [学习] GNSS信号跟踪环路原理、设计与仿真(仿真代码)
  • 关于汉语普通话元音音位最好归纳为几个的问题
  • 【Linux庖丁解牛】—系统文件I/O !
  • 【LRU】 (最近最少使用)