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

Spring Boot JAR部署至Wildfly 10服务器遇/hello3路径404问题求助

Spring Boot部署Wildfly指定context-root后404问题排查与解决

问题重现

将Spring Boot应用打包为WAR包,通过src/main/webapp/WEB-INF/jboss-web.xml指定context-root为/hello3,本地运行根路径/可返回"Hello World",但部署到Wildfly后访问/hello3持续返回404,而服务器上已存在的/hello1、/hello2路径运行正常。

排查与解决步骤

1. 确认Spring Boot WAR包的Servlet容器适配配置

Spring Boot应用部署到外部Servlet容器(如Wildfly)时,必须让启动类继承SpringBootServletInitializer并重写configure方法,否则容器无法正确加载Spring上下文:

@SpringBootApplication
public class Hello3Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Hello3Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Hello3Application.class, args);
    }
}

同时在pom.xml中设置打包方式为war,并排除内嵌Tomcat(避免与Wildfly容器冲突):

<packaging>war</packaging>

<dependencies>
    <!-- 排除内嵌Tomcat,由Wildfly提供容器支持 -->
    <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>
</dependencies>

2. 修正jboss-web.xml的兼容性配置

检查jboss-web.xml的schema版本是否与你的Wildfly版本匹配:

  • 旧版jboss-web_5_1.xsd仅适配JBoss AS 5/6,Wildfly 10+建议使用对应版本的schema,例如Wildfly 20+可改为:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/schema/jbossas/jboss-web_10_0.xsd">
    <context-root>/hello3</context-root>
</jboss-web>

同时确保文件路径为src/main/webapp/WEB-INF/jboss-web.xml,若项目默认没有webapp目录,需手动创建。

3. 检查Wildfly部署状态与日志

  • 登录Wildfly管理控制台(默认地址http://localhost:9990),查看hello3应用的部署状态是否为已启动。若状态异常,查看服务器日志(standalone/log/server.log),排查类加载冲突、依赖缺失等具体报错。
  • 确认部署的WAR包名称未与context-root冲突(例如WAR包名为hello3.war时,默认context-root也是/hello3,但配置文件优先级更高)。

4. 验证访问路径与端点映射

  • 部署后实际访问路径应为http://<服务器IP>:<端口>/hello3/(注意末尾斜杠),或直接访问http://<服务器IP>:<端口>/hello3。
  • 可在控制器中新增测试端点,排查是否为路径映射问题:
@RestController
public class HelloController {
    @GetMapping("/")
    public String hello() {
        return "Hello World";
    }

    @GetMapping("/test")
    public String test() {
        return "Test Success";
    }
}

尝试访问/hello3/test,若能返回内容,说明应用已正常启动,仅根路径映射需调整;若仍返回404,说明应用未正确加载。

5. 清理并重新部署

  • 删除Wildfly部署目录(standalone/deployments)下的旧WAR包及标记文件(如hello3.war.deployed)。
  • 执行Maven命令重新打包:mvn clean package,将新生成的WAR包部署到Wildfly。

内容的提问来源于stack exchange,提问作者Ho Quang Lam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 10:52:26