JDK21使用EasyExcel 3.0.3报错:module java.base does not “opens java.lang“ to unnamed module @22f71333
·
环境
JDK:21
Spring Boot:3.4.2
EasyExcel:3.0.3 (EasyExcel 3.1.0+版本不存在该问题)
报错
java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @22f71333
接口
@PostMapping("/import/users")
public List<UserExcelData> importUsers(@RequestParam("file") MultipartFile file) {
try {
UserExcelDataListener listener = new UserExcelDataListener();
EasyExcel.read(file.getInputStream(), UserExcelData.class, listener).sheet().doRead();
List<UserExcelData> users = listener.getUsers();
return users;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
解决方法
1、引入依赖
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>9.5.2</version>
</dependency>
2、在接口中添加一行代码,用于引入所有模块
StaticComponentContainer.Modules.exportAllToAll();
完整代码
@PostMapping("/import/users")
public List<UserExcelData> importUsers(@RequestParam("file") MultipartFile file) {
try {
StaticComponentContainer.Modules.exportAllToAll(); // 添加这行
UserExcelDataListener listener = new UserExcelDataListener();
EasyExcel.read(file.getInputStream(), UserExcelData.class, listener).sheet().doRead();
List<UserExcelData> users = listener.getUsers();
return users;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
更多推荐
所有评论(0)