Swagger实现在生产环境中使用,在发布时不使用
- 判断是不是生产环境 flag = false
- 注入enable(flag)
1、配置三个SpringBoot文件
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
spring.profiles.active=dev
- application-dev.properties
server.port=8081
- application-pro.properties
server.port=8082
2、编写SwaggerConfig配置类
package com.sgz.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment) {
Profiles profiles = Profiles.of("dev","test");
boolean flag = environment.acceptsProfiles(profiles);
System.out.println(flag);(这个)
ApiInfo apiInfo = new ApiInfoBuilder()
.contact(new Contact("我", "https://www.baidu.com", "xxxxxx@qq.com"))
.title("我的Swagger API开发文档")
.description("我的文档描述")
.version("1.1")
.build();
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.enable(flag)
.groupName("我")
.select()
.apis(RequestHandlerSelectors.basePackage("com.sgz.swagger.controller"))
.paths(PathSelectors.regex("/swagger/.*"))
.build();
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59