SpringBoot 2.x 升 3.x 避坑指南:企业级项目的实战问题与解决方案
spring boot 3.4.8 对应spring-cloud.version 多少适配
解决方法:
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope>
</dependency>
<spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version>
import javax.validation.Constraint; import javax.validation.Payload; 怎么找不到这两个包
解决方法
删除:
<!-- Bean Validation API(包含 javax.validation 相关接口) -->
<dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId>
</dependency><!-- 可选:添加Hibernate Validator作为运行时实现(如果需要实际校验功能) -->
<dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId>
</dependency>
替换成:
<!-- Jakarta Bean Validation API -->
<dependency><groupId>jakarta.validation</groupId><artifactId>jakarta.validation-api</artifactId><version>3.0.2</version> <!-- 对应JSR-380,适用于Jakarta EE 9+ -->
</dependency><!-- 运行时实现 -->
<dependency><groupId>org.hibernate.validator</groupId><artifactId>hibernate-validator</artifactId><version>7.0.5.Final</version>
</dependency>
无法访问jakarta.servlet.http.HttpServletRequest
解决方法:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!-- 无需指定版本,由 spring-boot-starter-parent 管理 -->
</dependency>
程序包org.aspectj.lang不存在 java版本是17 如何解决这个问题
解决方法
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.20.1</version> <!-- 使用最新稳定版本 -->
</dependency>
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.20.1</version>
</dependency>
httpSecurity.antMatchers() 找不到符号
解决方法
// 旧写法
httpSecurity.antMatchers("/public/**").permitAll().antMatchers("/admin/**").hasRole("ADMIN");// 新写法
httpSecurity.requestMatchers("/public/**").permitAll().requestMatchers("/admin/**").hasRole("ADMIN");
Java 17 应该选用flowable 版本多少
Flowable 6.8.x 及以上版本
问题
Springboot 版本升级为3.x 后,启动应用程序 报错 Application run failed java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes: - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
解决方法
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.20</version> <!-- 使用最新兼容版本 -->
</dependency>
问题
Springboot 版本升级为3.x 后,启动应用程序 报错 Invalid bean definition with name ‘shopProductMapper’ defined in file [D:\work\project\gitee-github\cherry-cloud\cherry-modules\cherry-modules-use\target\classes\com\ruoyi\use\mapper\ShopProductMapper.class]: Invalid value type for attribute ‘factoryBeanObjectType’: java.lang.String
解决方法:
<!-- MyBatis 与 Spring Boot 3.x 兼容的版本 -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version> <!-- 至少 3.0.0 以上版本 -->
</dependency>
问题
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId></dependency> 与 Spring Boot 3.x 兼容的版本 多少?
解决方法:
<!-- Pagehelper --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><exclusions><exclusion><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></exclusion></exclusions></dependency><!-- MyBatis 与 Spring Boot 3.x 兼容的版本 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version> <!-- 至少 3.0.0 以上版本 --></dependency>
Swagger2 和 Swagger3 有什么区别 和联系
- 若使用 Spring Boot 2.3.x 及以下:可使用 Springfox Swagger2(2.9.x)。
- 若使用 Spring Boot 2.4.x - 2.7.x:推荐 SpringDoc-OpenAPI 1.x(替代 Swagger2,支持 OpenAPI 3.0)。
- 若使用 Spring Boot 3.x 及以上:必须使用 SpringDoc-OpenAPI 2.x(完全兼容新特性)。
若使用 Spring Boot 3.x 及以上:必须使用 SpringDoc-OpenAPI 2.x(完全兼容新特性) maven 依赖怎么写
解决办法
<!-- SpringDoc OpenAPI 核心依赖 -->
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.1.0</version> <!-- 最新稳定版,支持 Spring Boot 3.x -->
</dependency>
- Swagger UI 界面(访问路径:
http://localhost:8080/swagger-ui/index.html
)
问题
import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; 如何改成 springdoc-openapi-starter-webmvc-ui 2.1.0 符合的注解
解决方法:
旧注解 (Swagger 2) | 新注解 (OpenAPI 3) | 说明 |
---|---|---|
@Api | @Tag | 用于描述控制器类 |
@ApiOperation | @Operation | 用于描述接口方法 |
@ApiImplicitParam | @Parameter | 用于描述请求参数 |
@ApiImplicitParams | @Parameters | 用于包含多个@Parameter |
- 其他常用注解替换:
@ApiParam
→@Parameter
(用于方法参数上)@ApiResponse
→@ApiResponse
(包路径变为io.swagger.v3.oas.annotations.responses
)@ApiResponses
→@ApiResponses
(包路径变为io.swagger.v3.oas.annotations.responses
)@ApiModel
→@Schema
(用于描述实体类)@ApiModelProperty
→@Schema
(用于描述实体类属性)
问题
Non-resolvable import POM: Failure to find org.springframework.cloud:spring-cloud-dependencies:pom:2023.0.1.2 in https://maven.aliyun.com/repository/public was cached in the local repository, resolution will not be reattempted until the update interval of public has elapsed or updates are forced @ line 51, column 25 -> [Help 2] [ERROR] ‘dependencies.dependency.version’ for org.springframework.cloud:spring-cloud-starter-bootstrap:jar is missing. 如何解决
解决方法
<spring-cloud.version>2023.0.1</spring-cloud.version><spring-cloud-alibaba.version>2023.0.1.0</spring-cloud-alibaba.version><!-- SpringCloud 微服务 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringCloud Alibaba 微服务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency>
问题
Application run failed java.lang.IllegalStateException: java.lang.NoClassDefFoundError: io/seata/rm/fence/SpringFenceConfig
解决方法
<dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.6.1</version> <!-- 使用 1.4.0+ 版本 -->
</dependency><!-- 如果需要分布式事务栅栏功能,可能还需要 -->
<dependency><groupId>io.seata</groupId><artifactId>seata-rm-fence</artifactId><version>1.6.1</version>
</dependency>
问题
Description: Field storageClient in com.ruoyi.file.service.FastDfsSysFileServiceImpl required a bean of type ‘com.github.tobato.fastdfs.service.FastFileStorageClient’ that could not be found. 这个问题如何解决
解决方法
@Autowired(required=false)private FastFileStorageClient storageClient;
问题
int org.springframework.web.reactive.function.client.ClientResponse.rawStatusCode()’ 报这个如何解决?
解决方法
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId><version>3.1.5</version> <!-- 使用最新稳定版 -->
</dependency>