首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【swagger关闭】生产环境关闭swagger方法

【swagger关闭】生产环境关闭swagger方法

作者头像
master336
发布2026-06-15 19:25:30
发布2026-06-15 19:25:30
1860
举报

swagger3 关闭配置(快捷方式)

配置参考

代码语言:javascript
复制
springfox:
  documentation:
    # 总开关(同时设置auto-startup=false,否则/v3/api-docs等接口仍能继续访问)
    enabled: false
    auto-startup: false
    swagger-ui:
      enabled: false

配置原理

代码语言:javascript
复制
<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-boot-starter</artifactId>
</dependency>

springfox.boot.starter.autoconfigure.OpenApiAutoConfiguration springfox.boot.starter.autoconfigure.SpringfoxConfigurationProperties

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

swagger2 关闭配置

swagger2 关闭主要是根据条件使swagger 配置不再生效,如

方法一:@ConditionalOnProperty

代码语言:javascript
复制
Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .contact(new Contact("java乐园", "https://xxx.com", "test@163.com"))
                .version("1.0")
                .description("User API 描述")
                .build();
    }
 
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}

application.yml 中配置如下关闭:

代码语言:javascript
复制
swagger:
  # 只要不是true就不启用
  enable: false

其他基于Conditional的方式

方法二 @Profile

原理跟第一个差不多,只是判断条件不同(profile判断配置文件,也即的参数)

代码语言:javascript
复制
Configuration
@EnableSwagger2
@Profile("!prod")
public class Swagger2 extends WebMvcConfigurationSupport {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .contact(new Contact("java乐园", "https://xxx.com", "test@163.com"))
                .version("1.0")
                .description("User API 描述")
                .build();
    }
 
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}

方法三 @Value 配置Docket 失效办法

代码语言:javascript
复制
Configuration
@EnableSwagger2
public class Swagger2 extends WebMvcConfigurationSupport {

	@Value("{swagger2.enable:false}")
    private boolean enableSwagger;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
				.enable(enableSwagger)
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                .contact(new Contact("java乐园", "https://xxx.com", "test@163.com"))
                .version("1.0")
                .description("User API 描述")
                .build();
    }
 
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-02-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • swagger3 关闭配置(快捷方式)
    • 配置参考
    • 配置原理
  • swagger2 关闭配置
    • 方法一:@ConditionalOnProperty
    • 方法二 @Profile
    • 方法三 @Value 配置Docket 失效办法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档