部署SpringBoot项目至IBM WebSphere 9.0.12时遇空指针错误求助
Spring Boot 部署到 IBM WebSphere 9.0.12 出现 NullPointerException 解决方案
这个空指针异常由WebSphere自带的JBoss Weld(CDI容器)与Spring Boot依赖/生命周期适配冲突导致,以下是针对性解决步骤:
1. 排除冲突的Weld依赖
WebSphere已内置Weld实现,需移除Spring Boot自带的相关依赖避免版本冲突:
Maven配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet-shaded</artifactId> </exclusion> <exclusion> <groupId>org.jboss.weld</groupId> <artifactId>weld-core</artifactId> </exclusion> </exclusions> </dependency>
Gradle配置
implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.jboss.weld.servlet', module: 'weld-servlet-shaded' exclude group: 'org.jboss.weld', module: 'weld-core' }
2. 适配Java EE容器的WAR包部署
将项目改为WAR打包模式,并调整启动类适配WebSphere容器:
修改启动类
@SpringBootApplication public class YourApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(YourApplication.class); } public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
设置打包类型(Maven)
<packaging>war</packaging>
3. 调整WebSphere类加载与CDI配置
若上述步骤无效,可修改WebSphere应用类加载策略:
- 登录WebSphere管理控制台,进入应用程序 > 企业应用程序 > 你的应用 > 类加载和更新检测
- 将类加载顺序设置为父类加载器最后(Parent Last)
或在WEB-INF/ibm-web-ext.xml中禁用CDI自动扫描:
<web-ext xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd" version="1.0"> <context-root>/your-app-context</context-root> <cdi-enable>false</cdi-enable> </web-ext>
4. 添加WebSphere适配依赖
引入Spring Boot官方的WebSphere starter,自动处理兼容性配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websphere</artifactId> </dependency>
完成上述配置后重新打包部署,即可解决该空指针异常。
内容的提问来源于stack exchange,提问作者Hari
相关产品推荐
相关产品推荐

