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

Spring Boot部署至Wildfly后Swagger UI无法正常运行问题求助

Spring Boot 2.7.3 + Swagger UI 部署Wildfly 10失败解决方案

核心原因

Wildfly 10自带的JAX-RS、JAXB等依赖与Swagger引入的同类型类发生类加载冲突,同时Spring Boot WAR包的默认配置未适配Wildfly的类加载机制,导致部署失败。

解决步骤

1. 调整Swagger依赖,排除冲突包

使用兼容Spring Boot 2.7.3的Swagger Starter,排除Wildfly已提供的依赖避免冲突。修改pom.xml:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2. 配置WAR打包适配Wildfly

标记Tomcat和Wildfly自带依赖为provided,避免重复打包:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>3.0.19.Final</version>
    <scope>provided</scope>
</dependency>

同时确保Spring Boot主类继承SpringBootServletInitializer:

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

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

3. 添加Wildfly部署描述符控制类加载

在src/main/webapp/WEB-INF创建jboss-deployment-structure.xml,排除服务器自带的冲突子系统和模块:

<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="jaxrs" />
        </exclude-subsystems>
        <exclusions>
            <module name="javax.ws.rs.api" />
            <module name="org.jboss.resteasy" />
        </exclusions>
        <local-last value="true" />
    </deployment>
</jboss-deployment-structure>

4. 验证Swagger配置类

确保Swagger配置使用兼容的注解,示例:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.your.package"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .title("API文档")
                        .version("1.0")
                        .build());
    }
}

完成以上配置后,重新打包WAR文件再部署到Wildfly 10即可解决冲突问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 17:25:28