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

Spring Boot替换内嵌Tomcat为Wildfly后出现404错误求助

Spring Boot部署到Wildfly出现404问题

我将Spring Boot项目的内嵌Tomcat替换为Wildfly服务器后,访问接口时出现404错误,但使用Tomcat时运行正常。已尝试重新编译、检查依赖、更换不同版本Wildfly等方法,均未解决问题。从日志中发现,Wildfly和Tomcat的认证过滤器打印的request.getRequestURI()均为/reports/api/system。

相关代码

主类

@SpringBootApplication
public class ReportApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ReportApplication.class);
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);

    }

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

控制器类

@RestController
@RequestMapping("api/system")
public class SystemController {

    SystemService systemService;

    public SystemController(SystemService systemService) {
        this.systemService = systemService;
    }

    @GetMapping
    public List<System> findAll() {
        return systemService.findAll();
    }
}

pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
<!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    </parent>

    <groupId>com.report</groupId>
    <artifactId>reports</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.reactivestreams</groupId>
            <artifactId>reactive-streams</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-models</artifactId>
            <version>2.2.9</version>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.2.9</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <!--            <version>42.3.8</version>-->
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

排查建议

  • 确认Wildfly中WAR包的上下文路径是否为/reports,访问的完整URL是否为http://<wildfly-host>:<port>/reports/api/system
  • 查看Wildfly的服务器日志,确认Spring Boot容器是否正常启动,控制器类是否被Spring成功扫描并注册
  • 移除pom.xml中的javax.javaee-api依赖,避免与Spring Boot自带的Servlet API或Wildfly内置的API发生冲突
  • 检查Wildfly版本与Spring Boot 2.7.1的兼容性,建议使用Wildfly 26.x或27.x版本(兼容Jakarta EE 8)
  • 检查Spring Security配置,确认是否对/api/system路径设置了正确的权限规则,避免请求被拦截后未转发到DispatcherServlet
  • 验证WAR包是否正确构建,解压WAR包检查WEB-INF/classes下是否包含编译后的类文件,WEB-INF/lib下的依赖是否完整

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 19:52:03