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();
}
}
|