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

配置springfox.documentation.enabled=false无法禁用Swagger 3.0求解决

解决生产环境禁用springdoc-openapi-ui(Swagger 3.0)的问题

你遇到的问题核心是用错了配置项:你引入的是org.springdoc的Swagger 3.0兼容依赖,但使用了springfox专属的配置参数,因此不会生效。以下是三种可行的解决思路:

1. 使用springdoc专属配置项直接禁用

在application-prod.properties中添加以下配置,分别关闭API文档接口和Swagger UI界面:

# 关闭OpenAPI文档接口(路径:/v3/api-docs)
springdoc.api-docs.enabled=false
# 关闭Swagger UI界面(路径:/swagger-ui.html)
springdoc.swagger-ui.enabled=false

2. 通过Maven控制生产环境不打包Swagger依赖

如果希望生产环境完全剔除Swagger相关代码,避免资源占用,可通过Maven Profile实现:
在pom.xml中添加环境Profile,生产环境下将Swagger依赖设为provided(不打包进最终产物):

<profiles>
    <profile>
        <id>prod</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-ui</artifactId>
                <version>1.6.4</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

打包生产环境包时执行命令:mvn clean package -Pprod

3. 通过@Profile注解控制Swagger配置类生效范围

如果项目中有自定义的Swagger配置类,直接为配置类添加@Profile({"!prod"})注解,使其仅在非生产环境加载:

@Configuration
@Profile({"!prod"}) // 仅在非prod环境生效
public class OpenApiConfig {
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("系统API文档")
                        .version("1.0")
                        .description("API接口说明"));
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 12:33:32