首先导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置config内容
package tzy.store.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.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
@Configuration
@EnableOpenApi
public class SwapperConfig {
@Bean
public Docket docket1(Environment environment){
//需要多少分组就写多少个Docket
return new Docket(DocumentationType.SWAGGER_2).groupName("test");
}
@Bean//配置docket以配置Swagger具体参数
public Docket docket(Environment environment) {
//设置显示swagger的环境
Profiles of=Profiles.of("dev","test");
//判断当前是否处于该环境
//通过enable()接受此参数判断是否要显示
boolean b = environment.acceptsProfiles(of);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("tzy")
.enable(b)
.select()
/*
* RequestHandleSelectors,配置要扫描接口的方式
* basePackage:指定要扫描的包
* any():扫描全部
* none():不扫描
* withClassAnnotation:扫描类上注解,参数是一个注解的反射对象
* withMethodAnnotation:扫描方法上的注解
**/
.apis(RequestHandlerSelectors.basePackage("tzy.example.controller"))
/*
* paths(),过滤什么路径
**/
// .paths(PathSelectors.ant("/hello/**"))
.build();
}
//配置文档信息
private ApiInfo apiInfo() {
Contact contact = new Contact("tzy", "https://www.cnblogs.com/TPureZY/", "2036139108@qq.com");
return new ApiInfo(
"似此星辰非昨夜,为谁风露立中宵。", // 标题
"Swagger", // 描述
"v1.0", // 版本
"http://terms.service.url/组织链接", // 组织链接
contact, // 联系人信息
"Apach 2.0 许可", // 许可
"许可链接", // 许可连接
new ArrayList<>()// 扩展
);
}
}