Spring 5.3.10应用从Java8升级到Java11时遇RuntimeException
解决方案:Java 11升级后Jetty+ASM扫描类报错问题
问题根源
这个错误的核心原因是Jetty使用的ASM版本不支持Java 11的字节码版本(版本号55),即便你手动引入了ASM 9.1,Jetty自身依赖的旧版ASM可能依然被优先加载,导致版本冲突;另外也可能是你的Jetty版本本身不兼容Java 11。
具体解决步骤
1. 升级Jetty到兼容Java 11的版本
Spring 5.3.10完全兼容Java 11,你需要同步升级Jetty到支持Java 11的版本:
- Jetty 9.4.40及以上版本完全支持Java 11
- 若使用Jetty 10+,需适配Servlet 4.0+规范
在pom.xml中更新Jetty核心依赖版本,示例:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.52.v20230823</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.4.52.v20230823</version> </dependency>
2. 统一ASM依赖版本,彻底避免冲突
不要单独零散引入ASM依赖,而是通过Maven的dependencyManagement强制全局使用兼容版本,同时排除Jetty自带的旧版ASM:
<dependencyManagement> <dependencies> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>9.1</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm-tree</artifactId> <version>9.1</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm-util</artifactId> <version>9.1</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm-commons</artifactId> <version>9.1</version> </dependency> </dependencies> </dependencyManagement>
同时在Jetty注解依赖中排除自带的ASM:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-annotations</artifactId> <version>9.4.52.v20230823</version> <exclusions> <exclusion> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> </exclusion> <exclusion> <groupId>org.ow2.asm</groupId> <artifactId>asm-tree</artifactId> </exclusion> <exclusion> <groupId>org.ow2.asm</groupId> <artifactId>asm-util</artifactId> </exclusion> </exclusions> </dependency>
3. 验证依赖树,确保无旧版ASM残留
执行Maven命令检查依赖树,确认所有ASM依赖都是指定的9.1版本:
mvn dependency:tree | grep asm
如果仍有旧版本ASM被引入,找到对应的依赖项并添加排除规则。
4. 临时应急方案(不推荐长期使用)
如果暂时无法升级Jetty,可以配置Jetty跳过报错类的扫描:
- 代码方式配置:
webAppContext.getAnnotationConfiguration().setExcludePatterns("your/package/path/Acl.class");
- web.xml配置:
<context-param> <param-name>org.eclipse.jetty.annotations.AnnotationParser.excludePatterns</param-name> <param-value>your/package/path/Acl.class</param-value> </context-param>
内容的提问来源于stack exchange,提问作者Ankitha Mohan
相关产品推荐
相关产品推荐

