原因:
在生產環境下,我們需要關係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();
}
}