Spring Boot 禁用 Swagger 的三种方式

阅读目录(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
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 项目中,可以通过添加配置来禁用 Swagger。 1. 针对整个应用禁用 Swagger 可以在应用的 `application.properties` 文件中添加以下配置: ```properties springfox.documentation.enabled=false ``` 或者在应用的启动类上添加 `@EnableSwagger2` 注解,并重写 `addResourceHandlers` 方法,将 Swagger UI 的访问路径重定向到 `/error`: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableSwagger2 public class SwaggerConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/") .setCachePeriod(0); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(0); registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .setCachePeriod(0); registry.addResourceHandler("/swagger-resources/**") .addResourceLocations("classpath:/META-INF/swagger-resources/") .setCachePeriod(0); } } ``` 2. 针对指定的 API 禁用 Swagger 可以在 API 的实现类上添加 `@ApiIgnore` 注解,例如: ```java @RestController @RequestMapping("/api") @ApiIgnore public class MyController { // ... } ``` 这样,Swagger 将不会显示该 API。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值