Spring Boot 项目中禁用 Swagger方法总结

如果你想在Spring Boot 项目中禁用 Swagger,有几种可能的方法。以下是一些建议:

方法一:application.properties 或 application.yml

在`application.properties`或`application.yml`文件中添加以下配置:

1
springfox.documentation.swagger.v2.enabled: false

这将禁用 Swagger 的自动配置。

方法二:使用条件注解

修改`SwaggerConfig`类,使用条件注解来控制是否启用 Swagger。例如:

1
2
3
4
5
6
@Configuration
@ConditionalOnProperty(name = "swagger.enabled", havingValue = "true", matchIfMissing = true)
@EnableSwagger2
public class SwaggerConfig {
// Swagger配置代码
}

在`application.properties`或`application.yml`中添加如下配置:

1
swagger.enabled: false

这样做将根据配置来决定是否启用 Swagger。

方法三:使用 Profile

在`SwaggerConfig`类中添加`@Profile`注解:

@Configuration
@EnableSwagger2
@Profile("!production")
public class SwaggerConfig {
// Swagger配置代码
}

在生产环境中,不激活该配置文件。

方法四:使用 exclude 属性

在`@SpringBootApplication`注解上使用`exclude`属性排除`SwaggerConfig`:

1
2
3
4
5
6
@SpringBootApplication(exclude = SwaggerConfig.class)
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}

方法五:使用enable属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(false)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ls.uem.emer.command.server.rest"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("xxxxx接口文档")
                        .description("xxxxxx接口文档,详细信息......")
                        .version("9.0")
                        .build());
    }
}

以上方法中,你可以选择最适合你项目的一种或组合使用。

posted @   夏威夷8080  阅读(6113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
历史上的今天:
2018-01-02 springboot中配置文件application.properties的理解
2018-01-02 restTemplate设置访问超时
2018-01-02 BigDecimal.setScale 处理java小数点
点击右上角即可分享
微信分享提示