阅读目录(Content)

回到顶部(go to top)

本文来讨论在 Spring Boot 中禁用swagger

原文:https://blog.csdn.net/weixin_37264997/article/details/82762050

一、方法一:使用@Profile

使用注解@Profile({“dev”,“test”})表示在开发或测试环境开启,而在生产关闭。(推荐使用)


@Configuration
@EnableSwagger2
@Profile({"local", "dev"})
public class Swagger2Config {
    @Bean
    public Docket swaggerPersonApi10() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .version("1.0")
                .title("xx项目:xx平台 Swagger2 文档 API")
                .contact(new Contact("  xx团队", "https://www.xx.com/", "kangjia@xx.com"))
                .description("logservice platform API v1.0")
                .build();
    }
}

二、方法二:使用 @Value() 推荐使用

  1. Swagger2Config类里添加

    
    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
        
        @Value("${swagger.enable}")
        private Boolean enable;
        
        @Bean
        public Docket swaggerPersonApi10() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller"))
                    .paths(PathSelectors.any())
                	.enable(enable)    //配置在该处生效
                    .build()
                    .apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .version("1.0")
                    .title("xx项目:xx平台 Swagger2 文档 API")
                    .contact(new Contact("  xx团队", "https://www.xx.com/", "kangjia@xx.com"))
                    .description("logservice platform API v1.0")
                    .build();
        }
    }
    
  2. 在配置文件里添加一个swagger.enable属性,根据不同的application-xx.yml进行动态插入truefalse即可。

三、方法三:使用@ConditionalOnProperty

  1. 使用注解@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)

    
    @Configuration
    @EnableSwagger2
    //@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = true   //matchIfMissing=true :为空则设为true,不合理
    @ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)
    public class Swagger2Config {
        
        @Bean
        public Docket swaggerPersonApi10() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller"))
                    .paths(PathSelectors.any())
                	.enable(enable)
                    .build()
                    .apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .version("1.0")
                    .title("xx项目:xx平台 Swagger2 文档 API")
                    .contact(new Contact("  xx团队", "https://www.xxx.com/", "kangjia@xxx.com"))
                    .description("logservice platform API v1.0")
                    .build();
        }
    }
    
  2. 然后在测试配置或者开发配置中 添加 swagger.enable = true 即可开启,生产环境不写该配置则默认关闭Swagger。

    #Swagger lock
    swagger:
        enabled: true
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐