项目需要,springboot需要升级到最新版本。
前提
原来的运行环境为JDK8,需要升级为JDK17
1.不兼容升级,IDEA直接全局替换
javax.servlet ===> jakarta.servlet
javax.validation ===> jakarta.validation
javax.annotation.Resource ===> jakarta.annotation.Resource
javax.mail ===> jakarta.mail
2.LegacyCookieProcessor被删除替换
LegacyCookieProcessor ===> Rfc6265CookieProcessor
注:换成Rfc6265CookieProcessor后,setCookie时domain不能用.开头
3.加密算法抛出异常问题
AES/GCM/PKCS5Padding ===> AES/GCM/NoPadding
切换JDK17后,代码中使用如下方式初始化Cipher时,会抛出异常:NoSuchAlgorithmException,将填充模式改成NoPadding即可
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding")
4.用到的一些jar包升级适配
grpc版本升级
grpc-spring-boot-starter.version 2.12.0.RELEASE===>3.0.0.RELEASE
protobuf-java-util 3.25.3===>1.58.0
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>${grpc-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>${protocol.buffer.version}</version>
<exclusions>
<exclusion>
<artifactId>error_prone_annotations</artifactId>
<groupId>com.google.errorprone</groupId>
</exclusion>
</exclusions>
</dependency>
shardingsphere5.4.1版本适配
适配的demo项目下载地址
shardingsphere 4.1.1===>5.4.1
旧版本:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
新版本:
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>${shardingsphere.version}</version>
<exclusions>
<exclusion>
<artifactId>shardingsphere-infra-util</artifactId>
<groupId>org.apache.shardingsphere</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-infra-spi</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
注:由于最新版本的shardingsphere5.4.1与snakeyaml2以上的版本不兼容,目前只能对其不兼容的部分按照官方的修复方法修改。
临时方案:排除pom文件中shardingsphere-jdbc-core中的shardingsphere-infra-util包,将其代码用本地的代码代替。另外需导入shardingsphere-infra-spi包。待后续官方版本出到5.5.0后再切换。
mysql相关升级
mybatis 2.1.4==>3.0.3
mybatis-plus 3.5.2==>3.5.5
pagehelper 1.4.3==>2.1.0
5.redis及redisson
redis配置变更:spring.redis前缀改成spring.data.redis前缀
spring:
data:
redis:
database:
host:
port:
username:
password:
redisson升级版本后,默认的序列化方式由MarshallingCodec变成了Kryo5Codec,导致原有缓存无法解析,需手动设置为MarshallingCodec。
// MarshallingCodec被标注为废弃,此处临时启用原有的序列化方式
if (config.getCodec() == null) {
config.setCodec(new MarshallingCodec());
}
6.开启尾部斜杠匹配
Spring Boot 3 对“尾斜线匹配”配置选项进行了修改。以前带不带尾部斜杠都可以命中,现在默认是严格区分。
WebMvcConfigurer接口实现类中添加:
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseTrailingSlashMatch(true);
}
7.模块化问题解决
报错1
程序包 sun.security.util 已在模块 java.base 中声明, 但该模块未将它导出到未命名模块
(Package ‘sun.security.util’ is declared in module ‘java.base’, which does not export it to the unnamed module)
java启动命令加入–add-opens命令,例如:
--add-opens java.base/sun.security.util=ALL-UNNAMED
编译插件改成:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>--add-exports=java.base/sun.security.util=ALL-UNNAMED</arg>
</compilerArgs>
...
</configuration>
</plugin>
报错2
不允许在使用 --release 时从系统模块 java.base 导出程序包
原因:spring-boot-starter-parent默认带有
<maven.compiler.release>${java.version}</maven.compiler.release>
解决方法:需要在pom中把属性覆盖了:
<properties>
<java.version>17</java.version>
<maven.compiler.release></maven.compiler.release>
...
</properties>
8.@Async修饰的异步方法返回值类型限制
Spring6.x中对@Async修饰的异步方法返回值类型做了限制
原来下面的方法是可以跑的(但是返回值为null)
controller:
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private TestService testService;
@GetMapping("/tt")
public String tt() {
String aa = testService.test();
log.info("输出结果:{}", Objects.isNull(aa));
return "success";
}
}
service
public interface TestService {
String test();
}
service实现类
@Slf4j
@Service
public class TestServiceImpl implements TestService {
@Async
@Override
public String test() {
return "aa";
}
}
输出结果
升级到springboot3.2.3后会报错
java.lang.IllegalArgumentException: Invalid return type for async method (only Future and void supported): class java.lang.String
需要把方法返回值改成Future类型或void
controller方法
@GetMapping("/bb")
public String bb() throws ExecutionException, InterruptedException {
Future<String> aa = testService.test();
log.info("输出结果:{}", aa.get());
return "success";
}
service方法
@Async
@Override
public CompletableFuture<String> test() {
log.info("start");
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
log.info("end");
return CompletableFuture.completedFuture("aa");
}
====================================================================
更新于2025年2月6日
Shardingsphere最新版本已经升级至5.5.1,直接升级至最新的官方版本即可,不需要对5.4.1进行代码改动了