升级SpringBoot2.x(jdk1.8)到SpringBoot3.x(jdk21)

版本

  • SpringBoot 3.2.5
  • jdk21

前言

javax

Servlet、JPA 等从 javax 迁移到 jakarta 命名空间。

spring

@PathVariable必须指定参数名

  @PathVariable Long projectCode  错误
  @PathVariable(value ="projectCode" ) Long projectCode 正确

mybatis plus

升级 Mybatis-Plus 版本为 3.5.5 版本,需要注意下 Maven 的坐标标识是mybatis-plus-spring-boot3-starter,这点和SpringBoot 2 的依赖坐标mybatis-plus-boot-starter有所区别。

hibernate-validator

类不存在

升级过程

【升级】springboot升级为3.2.5

截止到2024-05-14最稳定版本为3.2.5 故选择该版本

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.5</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

【改造】javax.annotation-api->jakarta.annotation-api

spring6开始 Servlet、JPA 等从 javax 迁移到 jakarta 命名空间。

Jakarta EE 和 javax 是 Java 企业版规范的发展,以前称为 Java EE(Java Platform, Enterprise Edition)和 J2EE(Java 2 Platform, Enterprise Edition)。Jakarta EE 实际上是 Java EE 在 Eclipse 基金会的管理下的新品牌和继续发展,这是在甲骨文将 Java EE 转让给它们之后的情况。由于 “Java” 名称的商标问题,这种改变需要重新命名,“Java” 名称仍由 Oracle 拥有 (Baeldung)。

这一转变中最显著的变化之一是命名空间从 javax.* 切换到 jakarta.*。这一变化从 Jakarta EE 9 开始正式实施,对开发者来说是一个重大转变,因为所有以前的 javax 命名空间下的规范和应用程序都需要过渡到 jakarta (Jakarta EE)。例如,以前的 javax.servlet 现在变为 jakarta.servlet。这种重命名影响到 Java 企业开发的所有方面,从应用服务器到构建工具和框架,这些都已更新以适应新的命名空间 (JetBrains)。

从 javax 到 jakarta 的过渡还涉及更新项目依赖,这可能因库名称和版本的变化而复杂。工具如 Eclipse Transformer 可以帮助迁移应用程序,通过转换现有的 javax 类和包到 jakarta (DZone) (JetBrains)。

总体而言,向 Jakarta EE 的过渡代表了向开放、社区驱动的过程的广泛转变,用于发展企业 Java 规范,提供一个平台,继续支持在现代、分布式计算环境中的大规模服务器端应用 (The Enterprisers Project)。

  1. 添加 jakarta.servlet 依赖
<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.0</version>
</dependency>
  1. 修改项目内所有代码的导入依赖
//修改前:
import javax.servlet.*
//修改后:
import jakarta.servlet.*

【异常】java: java.lang.NoSuchFieldError: Class com.sun.tools.javac.tree.JCTree$JCImport does not have member field ‘com.sun.tools.javac.tree.JCTree qualid’

lombok版本兼容性的问题
Maven依赖更新新版 lombok 为 1.18.30以上 即可

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.30</version>
  <scope>provided</scope>
</dependency>

【异常】java: 无法访问javax.validation.ConstraintValidator

找不到javax.validation.ConstraintValidator的类文件

修改hibernate-validator版本为8.0.1.Final

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>8.0.1.Final</version>
</dependency>

【异常】Exception in thread “main” java.lang.AbstractMethodError: Receiver class org.springframework.boot.logging.logback.RootLogLevelConfigurator does not define or inherit an implementation of the resolved method ‘abstract void configure(ch.qos.logback.classic.LoggerContext)’ of interface ch.qos.logback.classic.spi.Configurator.

logback版本导致,修改为下面版本

<dependency>
    <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>2.0.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>2.0.13</version>
    </dependency>
    <!-- logback -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
    <version>1.5.6</version>
</dependency>

如果出现下面的错误 也是同上

SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J(W): Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J(W): Ignoring binding found at [jar:file:/D:/develop/apache-maven-3.8.1/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J(W): See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.

【异常】Invalid value type for attribute ‘factoryBeanObjectType’: java.lang.String

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
[ERROR] 2024-05-14 17:33:04.215 +0800 org.springframework.boot.SpringApplication:[851] - Application run failed
java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getTypeForFactoryBeanFromAttributes(FactoryBeanRegistrySupport.java:86)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:837)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:652)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:575)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:534)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:138)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
	at com.dolphin.api.ApiApplicationServer.main(ApiApplicationServer.java:41)
[ERROR] 2024-05-14 17:33:04.217 +0800 com.dolphin.common.thread.DefaultUncaughtExceptionHandler:[32] - Caught an exception in Thread[#1,Api-Server,5,main].
java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getTypeForFactoryBeanFromAttributes(FactoryBeanRegistrySupport.java:86)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:837)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:652)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:575)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:534)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:138)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
	at com.dolphin.api.ApiApplicationServer.main(ApiApplicationServer.java:41)

调整mybatis-plus版本为3.5.5以上,具体参考Spring Boot 升级 3.2 报错 Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String

所以升级 Mybatis-Plus 版本为 3.5.5 版本即可,需要注意下 Maven 的坐标标识 是mybatis-plus-spring-boot3-starter,这点和SpringBoot 2 的依赖坐标mybatis-plus-boot-starter有所区别。

 <!-- mybatis -->
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
   <version>3.5.5</version>
</dependency>
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus</artifactId>
   <version>3.5.5</version>
</dependency>
<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-annotation</artifactId>
   <version>3.5.5</version>
</dependency>

【异常】java.lang.IllegalStateException: Could not resolve parameter [1] in public com.dolphin.api.utils.Result com.dolphin.api.exceptions.ApiExceptionHandler.exceptionHandler(java.lang.Exception,org.springframework.web.method.HandlerMethod): No suitable resolver

由于在参数上未显示使用注解来指定参数名,而编译时没有保留方法参数名,导致无法确定参数名,所以才报错。

在这里插入图片描述

需要指定参数名

在这里插入图片描述

附录(pom.xml)

建议单独建一个bom模块,统一管理所有外部依赖版本

<properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <spring-boot.version>3.2.5</spring-boot.version>
     <mysql-connector.version>8.0.33</mysql-connector.version>
     <mybatis-plus.version>3.5.5</mybatis-plus.version>
     <slf4j.version>2.0.13</slf4j.version>
     <logback.version>1.5.6</logback.version>
     <jackson.version>2.16.1</jackson.version>
     <commons-lang3.version>3.12.0</commons-lang3.version>
     <commons-collections4.version>4.4</commons-collections4.version>
     <guava.version>33.0.0-jre</guava.version>
     <hibernate-validator.version>8.0.1.Final</hibernate-validator.version>
     <commons-io.version>2.11.0</commons-io.version>
     <jakarta.annotation.version>2.1.0</jakarta.annotation.version>
 </properties>

 <!--    用于引入和利用外部定义的一套兼容版本集合,从而简化版本管理和提高依赖之间的兼容性-->
 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-parent</artifactId>
             <version>${spring-boot.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
             <version>${spring-boot.version}</version>
         </dependency>
         
         <dependency>
             <groupId>org.hibernate.validator</groupId>
             <artifactId>hibernate-validator</artifactId>
             <version>${hibernate-validator.version}</version>
         </dependency>

         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
             <version>${guava.version}</version>
         </dependency>

         <!-- mysql-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <version>${mysql-connector.version}</version>
             <scope>runtime</scope>
         </dependency>


         <!-- spring -->
         <!--Spring Boot的父POM依赖,用于提供默认的依赖管理和插件配置-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-parent</artifactId>
             <version>${spring-boot.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>

         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
             <version>${mybatis-plus.version}</version>
         </dependency>

         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus</artifactId>
             <version>${mybatis-plus.version}</version>
         </dependency>
         <dependency>
             <groupId>com.baomidou</groupId>
             <artifactId>mybatis-plus-annotation</artifactId>
             <version>${mybatis-plus.version}</version>
         </dependency>

         <!-- sl4j -->
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>${slf4j.version}</version>
         </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>${slf4j.version}</version>
         </dependency>
         <!-- logback -->
         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
             <version>${logback.version}</version>
         </dependency>

         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-core</artifactId>
             <version>${logback.version}</version>
         </dependency>

         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
             <version>${commons-lang3.version}</version>
         </dependency>

         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-collections4</artifactId>
             <version>${commons-collections4.version}</version>
         </dependency>

         <!-- jackson -->
         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-annotations</artifactId>
             <version>${jackson.version}</version>
         </dependency>

         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
             <version>${jackson.version}</version>
         </dependency>

         <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-core</artifactId>
             <version>${jackson.version}</version>
         </dependency>

         <dependency>
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>
             <version>${commons-io.version}</version>
         </dependency>

         <dependency>
             <groupId>jakarta.annotation</groupId>
             <artifactId>jakarta.annotation-api</artifactId>
             <version>${jakarta.annotation.version}</version>
         </dependency>

     </dependencies>
 </dependencyManagement>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值