导入swagger坐标
2版本坐标:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
3版本:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Swagger配置类
@Configuration
@EnableOpenApi //3版本
//@EnableSwagger2 //2版本
public class SwaggerConfig {
//配置Swagger的docket的bean实例
@Bean
public Docket docket(Environment environment) {
//设置要显示的配置环境(application-dev.properties)
Profiles of = Profiles.of("dev", "test");
//判断项目环境是否存在
boolean b = environment.acceptsProfiles(of);
System.out.println(b);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否启用swagger
.enable(b)
.groupName("111")
.select()
//RequestHandlerSelectors,配置要扫描接口的方式
//basePackage 指定包路径
//none 都不
//any 全部
//withClassAnnotation 类上的注解
//withMethodAnnotation 方法上的注解
.apis(RequestHandlerSelectors.basePackage("cn.it.controller"))
//扫描路径 ant表达式匹配
// .paths(PathSelectors.ant("/it/**"))
.build()
;
}
//配置多个Docket 进行分组
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("all");
}
//配置Swagger的信息的apiInfo
public ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("pgc", "https://blog.youkuaiyun.com/Simplg", "593631426@foxmail.com");
return new ApiInfo("Pancras_gc", "若没有梦想,何必远方",
"2.0", "http://www.baidu.com", contact,
"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
3.0:http://localhost:8888/swagger-ui/index.html
2.0:http://localhost:8080/swagger-ui.html