springboot(十一) 在必要的情況下關閉swagger配置

原因:

    在生產環境下,我們需要關係swagger配置,避免暴露接口這種危險的行爲。

方法:

1.使用@value()註解(配置文件爲properties的時候)

@Configuration
@EnableSwagger2
public class SwaggerConfig {


    @Value("swagger.enable")
    private String enable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .enable(Boolean.valueOf(enable))
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.example.test")).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(productName).version(version).build();
    }
}

2.@ConditionalOnProperty(value = { "swaggerui.enabled" }, matchIfMissing = true)。

@Configuration
@ConditionalOnProperty(value = { "swaggerui.enabled" }, matchIfMissing = true)
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.fang.industry")).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(productName).version(version).build();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章