生产中关闭swagger的几种方式

一.可以通过profile注解来处理。

Swagger的congif类上声明@Profile({"dev", "test"}),发布到生产上使用product的profile时, swagger是无效的。

二.

@Configuration 
@EnableSwagger2 
@EnableWebMvc 
@ComponentScan ( "com.XXX.controller"
public class SwaggerConfig{  
  @Autowired 
  ConfigService configService; 
    
  @Bean 
  public Docket customDocket() { 
   if (configService.getServerEnv() == ServerEnvEnum.ONLINE) { 
    return new Docket(DocumentationType.SWAGGER_2) 
    .apiInfo(apiInfoOnline()) 
   .select() 
     .paths(PathSelectors.none()) //如果是线上环境,添加路径过滤,设置为全部都不符合 
   .build(); 
   } else
    return new Docket(DocumentationType.SWAGGER_2) 
    .apiInfo(apiInfo()); 
  
 
   
  private ApiInfo apiInfo() { 
   return new ApiInfoBuilder() 
     .title( "XXX系统"
     .description( "XXX系统接口"
     .license( ""
     .licenseUrl( ""
     .termsOfServiceUrl( ""
     .version( "1.0.0"
     .contact( new Contact( "" , "" , "" )) 
     .build(); 
 
  private ApiInfo apiInfoOnline() { 
   return new ApiInfoBuilder() 
     .title( ""
     .description( ""
     .license( ""
     .licenseUrl( ""
     .termsOfServiceUrl( ""
     .version( ""
     .contact( new Contact( "" , "" , "" )) 
     .build(); 
 
}

三.

  1. 启动判断写到配置文件中,根据条件判断是否加载
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.show}")
    private boolean swaggerShow;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.xx.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RESTful APIs")
                .description("用于项目前端接口调用")
                .termsOfServiceUrl("")
                .contact("")
                .version("1.0")
                .termsOfServiceUrl("11")
                .build();
    }
}
  1. 资源文件是否扫描
@Configuration
@ComponentScan(value = "org.xx.interceptor")
class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Value("${swagger.show}")
    private boolean swaggerShow;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (this.swaggerShow) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");

            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }
}
  1. 配置文件中
swagger.show=true
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值