You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.x+Java 8升级至3.x+Java 21的最简方法及Open Rewrite Maven实操

升级Java 8+Spring Boot 2.x到Java 21+Spring Boot 3.x的方案

最简升级步骤

最简方式是手动调整依赖+代码适配,适合小型或依赖简单的项目:

  • 第一步:依赖兼容性排查
    先梳理项目中所有第三方依赖(如MyBatis、Redis客户端、数据库驱动等),确认它们是否有支持Java 21和Spring Boot 3.x的版本。比如MySQL驱动需要升级到8.0.30+,MyBatis-Plus要升级到3.5.3+。
  • 第二步:修改Maven配置
    1. 更新Spring Boot父依赖版本到稳定的3.x版(如3.2.5):
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>3.2.5</version>
          <relativePath/>
      </parent>
      
    2. 调整Java编译版本到21:
      <properties>
          <java.version>21</java.version>
          <maven.compiler.source>${java.version}</maven.compiler.source>
          <maven.compiler.target>${java.version}</maven.compiler.target>
      </properties>
      
      如果用到maven-surefire-plugin,确保测试用的JVM版本也是21。
    3. 若使用Spring Cloud,升级到对应版本(Spring Boot 3.x对应Spring Cloud 2022.0.x):
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-dependencies</artifactId>
                  <version>2022.0.4</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      
  • 第三步:代码适配
    • 替换javax包为jakarta:Spring Boot 3基于Jakarta EE 9,所有Java EE相关的包名从javax.*改成jakarta.*,比如javax.persistence.Entity → jakarta.persistence.Entity,javax.servlet.http.HttpServletRequest → jakarta.servlet.http.HttpServletRequest。
    • 移除过时API:比如Spring Security的WebSecurityConfigurerAdapter已被移除,改用SecurityFilterChain来配置安全规则:
      @Bean
      public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
          http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
              .formLogin(Customizer.withDefaults());
          return http.build();
      }
      
    • 处理其他过时方法:比如RestTemplate的部分方法标记为过时,可考虑替换为WebClient,或者继续使用但注意后续版本移除风险。
  • 第四步:验证测试
    运行所有单元测试、集成测试,启动项目检查日志中的警告和错误,逐一修复依赖冲突或代码问题。

使用Maven+Open Rewrite自动升级

Open Rewrite可以自动完成大部分依赖升级和代码替换,适合中型或大型项目:

  • 第一步:在pom.xml中添加Open Rewrite插件
    在build/plugins下加入以下配置,包含Spring Boot升级和Java版本升级的recipe:
    <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.40.0</version>
        <configuration>
            <activeRecipes>
                <!-- 升级Spring Boot到3.x -->
                <recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot3</recipe>
                <!-- 升级Java到21 -->
                <recipe>org.openrewrite.java.UpgradeJavaVersion</recipe>
            </activeRecipes>
            <properties>
                <javaVersion>21</javaVersion>
            </properties>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.openrewrite.recipe</groupId>
                <artifactId>rewrite-spring</artifactId>
                <version>5.40.0</version>
            </dependency>
        </dependencies>
    </plugin>
    
  • 第二步:预览升级变更
    运行以下命令查看Rewrite将要做出的修改,不会实际更改代码:
    mvn rewrite:dryRun
    
    查看target/rewrite/rewrite-report.html报告,确认所有变更符合预期。
  • 第三步:应用升级变更
    执行命令让Rewrite自动修改代码和配置:
    mvn rewrite:run
    
  • 第四步:手动补全未覆盖的内容
    Open Rewrite可能无法处理所有自定义代码或小众依赖的适配,需要手动检查并修复剩余问题,比如第三方依赖的版本调整、自定义注解的包名替换等。
  • 第五步:验证测试
    同最简步骤的第四步,运行测试并启动项目,确保所有功能正常。

内容的提问来源于stack exchange,提问作者sheetal mahandule

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:53:25