# SpringBoot2.6.x以上需要配置,springfox3.0.x以上需要配置
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# 激活哪个环境
spring.profiles.active=dev
# 生产环境
server.port=8081
# 发布环境
server.port=8082
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;
/**
* 日期:2022/8/15 - 15:54
* 需求:配置Swagger
*/
@Configuration // 标识是一个配置页面
@EnableSwagger2 // 标识开启Swagger,会扫描当前所在包以及子包中所有的注解
public class SwaggerConfig {
/**
* 创建Docket类型的对象,并使用Spring容器bean管理,Docket是Swagger中的全局配置对象
*
* @return
*/
@Bean
public Docket docket(Environment environment) {
// 设置要显示的Swagger环境,获取到则8081可以获取API,获取不到则8082无法获取API(这个)
Profiles profiles = Profiles.of("dev","test");
// 获取项目的环境,判断是否处在自己设定的环境中(这个)
boolean flag = environment.acceptsProfiles(profiles);
System.out.println(flag);(这个)
// API帮助文档的描述信息
ApiInfo apiInfo = new ApiInfoBuilder()
.contact(new Contact("我", "https://www.baidu.com", "xxxxxx@qq.com")) // 作者信息
.title("我的Swagger API开发文档") // 文档标题
.description("我的文档描述") // 文档描述
.version("1.1") // 版本号
.build(); // 构建
// 创建Docket类型的对象
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo) // API帮助文档的描述信息
.enable(flag) // 是否启动Swagger,false不能在浏览器中访问,true可以在浏览器中访问(这个)
.groupName("我") // 配置分组
.select() // 获取Docket中的选择器
.apis(RequestHandlerSelectors.basePackage("com.sgz.swagger.controller")) // 设置扫描包以及子包中的注解
.paths(PathSelectors.regex("/swagger/.*")) // 过滤路径,使用正则表达式约束生成API文档的路径地址
.build();
}
}