RESTEasy+Guice Servlet部署失败:this.modules为空异常排查求助
Tomcat 中 RESTEasy + Guice 部署失败排查
核心错误分析
1. 触发部署失败的根源前置错误
从localhost_access_log.2024-10-15.txt的栈轨迹可以明确:应用在初始化阶段就已失败,触发销毁流程时,GuiceResteasyBootstrapServletContextListener的this.modules字段为null,调用iterator()抛出空指针异常:
java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "this.modules" is null at dev.resteasy.guice.GuiceResteasyBootstrapServletContextListener.triggerAnnotatedMethods(GuiceResteasyBootstrapServletContextListener.java:131) at dev.resteasy.guice.GuiceResteasyBootstrapServletContextListener.contextDestroyed(GuiceResteasyBootstrapServletContextListener.java:127)
栈轨迹中LifecycleBase.start()调用stop()的逻辑,证明应用启动未完成就触发了销毁,而销毁时Listener的模块列表未完成初始化。
2. 连锁类加载异常(非根源)
catalina.2024-10-15.log中的IllegalStateException是后续连锁问题:应用停止后,异步线程仍在处理Guice的错误信息,尝试加载Guava的RegularImmutableSortedSet类,但此时Webapp类加载器已被标记为停止状态,因此抛出非法访问异常。这个异常是结果,不是问题根源。
排查与修复步骤
检查Guice模块注册配置
- 核对近期修改中是否调整了Guice模块的注册逻辑,比如删除了模块注册代码、修改了Listener的初始化参数。
- 确保
web.xml中正确配置模块列表,示例:<listener> <listener-class>dev.resteasy.guice.GuiceResteasyBootstrapServletContextListener</listener-class> <init-param> <param-name>resteasy.guice.modules</param-name> <param-value>com.your.package.YourModule1,com.your.package.YourModule2</param-value> </init-param> </listener> - 若通过代码注册模块,确认
GuiceResteasyBootstrapServletContextListener的modules字段未被意外置空。
定位Guice Injector创建失败原因
栈轨迹中出现Guice.CreationException,说明Guice创建Injector时失败,这是应用初始化停止的核心原因。由于异步日志截断了完整错误信息,需:- 调整Tomcat日志配置,将
com.google.inject的日志级别设为DEBUG,获取完整的依赖绑定错误信息。 - 检查近期新增的代码是否存在未绑定的依赖类,或修改了现有绑定的Scope、实现类。
- 调整Tomcat日志配置,将
排查类加载冲突
- 验证项目中Guava版本与Guice、RESTEasy依赖的Guava版本兼容,避免版本冲突导致类加载异常。
- 清理Tomcat的
work目录和项目target目录,重新编译打包部署,排除旧编译文件干扰。
验证Listener初始化逻辑
若自定义了GuiceResteasyBootstrapServletContextListener子类,检查近期修改是否破坏了父类初始化流程,导致modules列表未正确填充。
内容的提问来源于stack exchange,提问作者Roy Wood
相关产品推荐
相关产品推荐

