You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot War包部署至Liberty服务器失败问题排查

这种情况我碰到过好几次,大概率是War包的打包配置或者Liberty的部署设置出了问题,导致Spring Boot的上下文没被正确加载。咱们一步步排查解决:

1. 先确认Spring Boot的War打包配置是否正确

你的pom.xml必须做好War打包的适配,尤其是要处理内置容器和Spring Boot的启动逻辑:

<packaging>war</packaging>

<dependencies>
    <!-- 排除内置Tomcat,因为用Liberty作为外部容器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- 核心Web依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Security依赖(如果用到的话) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- 一定要指定你的Spring Boot主类全路径 -->
                <mainClass>com.yourpackage.YourApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这里的关键是spring-boot-maven-plugin的repackage目标——它会把普通War包转换成Spring Boot可识别的部署格式,同时标记内置Tomcat为provided,避免和Liberty冲突。

2. 检查Liberty服务器的部署配置

Eclipse运行时会自动帮你配置很多细节,但手动部署War包时得自己补全:

  • 确保Liberty的server.xml里启用了Spring Boot相关特性(版本要和你的Spring Boot对应,比如Spring Boot 3.x用springBoot-3.0):
<featureManager>
    <feature>springBoot-3.0</feature>
    <feature>webProfile-10</feature> <!-- 对应你的Java EE/Web Profile版本 -->
    <feature>security-1.0</feature> <!-- 如果涉及安全验证 -->
</featureManager>
  • 配置应用的上下文根,保证和你访问的URL匹配:
<webApplication id="your-app" location="your-app.war" contextRoot="/your-context"/>

如果没指定contextRoot,默认是War包的名称(比如myapp.war对应/myapp),访问时必须加这个前缀,否则会触发Liberty自身的默认安全验证(就是你看到的登录弹窗),而不是你的Spring配置。

3. 确认Spring Boot主类的启动配置

你的主类必须继承SpringBootServletInitializer,这样Liberty才能识别它作为Spring Boot应用的入口:

@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);
    }
}

如果没继承这个类,Liberty只会把你的War当成普通Web应用,根本不会加载Spring上下文,自然找不到你的自定义端点和Security配置。

4. 查看日志确认Spring是否启动

打开Liberty的server/logs/messages.log,搜索Spring Boot或者你的主类名:

  • 如果没有类似Started YourApplication in X seconds的日志,说明Spring完全没启动,回头检查前面的打包和配置;
  • 如果有启动日志,再找端点注册的日志(比如Mapped "{/your-endpoint}" onto public ...),如果看不到,可能是端点配置有问题,或者Security拦截了请求。
5. 排查Spring Security的冲突问题

如果Spring已经启动但还是弹出陌生的登录框,可能是Liberty的默认安全配置和你的Spring Security冲突了。可以先临时关闭Spring Security测试:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class YourApplication extends SpringBootServletInitializer {
    // ...
}

如果这样访问正常了,就说明是你的Security配置没生效或者规则不对,再重新调整你的Security配置类即可。

内容的提问来源于stack exchange,提问作者Mauro M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:34:02