package com.zzy.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.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;
import java.util.ArrayList;
/**
* @author ZhaoZhiyue
* @DATE 2020/12/10 - 0:14
*/
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置了Swagger的Docket的Bean实例
@Bean
public Docket docket(Environment environment) {
//设置要显示的swagger环境
Profiles profiles =Profiles.of("dev","test");
//获取项目的环境 判断是否处在自己设定的环境之中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//配置是否启动swagger
.enable(flag)
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage 制定要扫描的包
//any();扫描全部
//none();都不扫描
.apis(RequestHandlerSelectors.basePackage("com.zzy.swagger.controller"))
//过滤
// .paths(PathSelectors.ant("/zzy/**"))
.build();//build
}
//配置swagger信息=apifo
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("赵志岳","www.baidu.com","123@qq.com");
return new ApiInfo("ZZY的SwaggerAPI文档",
"不积跬步无以至千里",
"1.0",
"https://www.baidu.com/",
contact,
"Apache 2.0",
"https://www.baidu.com/",
new ArrayList());
}
}
依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>