Spring Boot 禁用 Swagger 的三种方式
Posted 林深时觉寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 禁用 Swagger 的三种方式相关的知识,希望对你有一定的参考价值。
本文来讨论在 Spring Boot 中禁用swagger
原文:https://blog.csdn.net/weixin_37264997/article/details/82762050
一、方法一:使用@Profile
使用注解@Profile({“dev”,“test”})
表示在开发或测试环境开启,而在生产关闭。(推荐使用)
package com.unidata.cloud.logservice.infra.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author kangjia@xx.com
* @date 2019/10/14 9:27
*/
@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() 推荐使用
在
Swagger2Config
类里添加package com.unidata.cloud.logservice.infra.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author kangjia@xx.com * @date 2019/10/14 9:27 */ @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(); } }
在配置文件里添加一个
swagger.enable
属性,根据不同的application-xx.yml
进行动态插入true
或false
即可。
三、方法三:使用@ConditionalOnProperty
使用注解
@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)
package com.unidata.cloud.logservice.infra.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author kangjia@xx.com * @date 2019/10/14 9:27 */ @Configuration @EnableSwagger2 @ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = 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(); } }
然后在测试配置或者开发配置中 添加
swagger.enable = true
即可开启,生产环境不填则默认关闭Swagger。#Swagger lock swagger: enabled: true
以上是关于Spring Boot 禁用 Swagger 的三种方式的主要内容,如果未能解决你的问题,请参考以下文章
Swagger + spring boot + jwt + 如何禁用特定 API 的授权按钮
Spring Boot Security - 如果用户未通过 Oauth2 进行身份验证,如何禁用对 swagger ui 页面的端点的访问