阅读文本大概需要3分钟。
0x01:使用@ConditionalOnProperty注解
使用注解
@ConditionalOnProperty(name = "swagger2.enable", havingValue = "true")
然后在测试配置或者开发配置中添加swagger2.enable = true 即可开启,生产环境不填则默认关闭Swagger。
@Configuration @EnableSwagger2 @ConditionalOnProperty(name = "swagger2.enable", havingValue = "true") public class Swagger2 extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 测试使用 Swagger2 构建RESTful API") .contact(new Contact("java乐园", "https://xxx.com", "test@163.com")) .version("1.0") .description("User API 描述") .build(); } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } }
在application.yml配置文件添加,启用swagger
swagger2.enable: true
0x02:使用注解@Profile
@Configuration @EnableSwagger2 @Profile("dev") public class Swagger2 extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 测试使用 Swagger2 构建RESTful API") .contact(new Contact("java乐园", "https://xxx.com", "test@163.com")) .version("1.0") .description("User API 描述") .build(); } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } }
0x03:使用Docket的enable方法
@Configuration @EnableSwagger2 public class Swagger2 extends WebMvcConfigurationSupport { @Value("{swagger2.enable:false}") private boolean enableSwagger; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(enableSwagger) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.yq.demo.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 测试使用 Swagger2 构建RESTful API") .contact(new Contact("java乐园", "https://xxx.com", "test@163.com")) .version("1.0") .description("User API 描述") .build(); } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } }
在application.yml配置文件添加,启用swagger
swagger2.enable: true
生成环境禁用Swagger非常必要,防止服务器的全部接口暴露给互联网。因为如果全部的接口暴露给互联网,就存在非常严重的安全隐患。容易受到黑客的攻击。
☆
往期精彩
☆
01 漫谈发版哪些事,好课程推荐
02 Linux的常用最危险的命令
03 精讲Spring Boot—入门+进阶+实例
04 优秀的Java程序员必须了解的GC哪些
05 互联网支付系统整体架构详解
关注我
每天进步一点点
喜欢!在看☟