Spring Boot应用部署到WildFly 7后REST端点无法访问
解决方案:Spring Boot 2.7 WAR部署JBoss 7x后REST接口404问题
针对你遇到的DemoApplication的configure方法未调用、组件未扫描、接口404的问题,可按以下步骤排查修复:
1. 修正Spring Boot启动类结构
确保启动类正确继承SpringBootServletInitializer并实现configure方法,这是WAR包部署到外部容器的核心要求:
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { // 指定启动类,确保Spring上下文正确初始化 return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
如果控制器不在启动类的包或子包下,需手动指定组件扫描范围:
@SpringBootApplication(scanBasePackages = "com.your.controller.package")
2. 配置JBoss避免容器冲突
在src/main/webapp/WEB-INF下创建jboss-web.xml,明确上下文根并隔离JBoss自带组件,让Spring Boot接管请求:
<jboss-web> <!-- 对应你访问的/demo上下文 --> <context-root>/demo</context-root> <deployment-structure> <deployment> <!-- 排除JBoss自带的Web服务子系统,避免与Spring MVC冲突 --> <exclude-subsystems> <subsystem name="webservices" /> </exclude-subsystems> <!-- 排除JBoss的Servlet API模块,使用Spring Boot提供的兼容版本 --> <exclusions> <module name="javax.servlet.api" /> </exclusions> </deployment> </deployment-structure> </jboss-web>
3. 调整WAR打包依赖配置
在pom.xml中确保打包类型为WAR,并将Tomcat依赖设为provided,避免与JBoss容器冲突:
<packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat依赖设为provided,由JBoss提供容器支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- Actuator依赖需确保配置正确 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
4. 修复Jandex索引问题
部署时的Jandex警告可能导致组件扫描失效,添加Jandex插件强制生成类索引:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.jboss.jandex</groupId> <artifactId>jandex-maven-plugin</artifactId> <version>1.2.3</version> <executions> <execution> <id>make-index</id> <goals> <goal>jandex</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
5. 验证部署日志
查看JBoss的server.log:
- 确认存在Spring Boot启动日志(如
Started DemoApplication in X seconds),若没有说明应用未初始化,需检查上述配置 - 确认存在组件扫描日志(如
Scanning for annotated classes in [xxx]),验证控制器是否被正确扫描
内容的提问来源于stack exchange,提问作者Leos Literak
相关产品推荐
相关产品推荐

