一.可以通过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();
}
}
|
三.
- 启动判断写到配置文件中,根据条件判断是否加载
@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();
}
}
- 资源文件是否扫描
@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/");
}
}
}
- 配置文件中
swagger.show=true