1.1流行性
- 号称世界上最流行的api框架
- Restful api文档在线生成工具=》api文档与api定义同步更新
- 直接运行,可以在线测试api接口
- 支持多种语言
官网
swagger官网
1.2依赖
在项目中使用swagger需要使用springfox,依赖
- springfox swagger2
- springfox ui
1.3springboot整合swagger
我这里用已经有的项目演示
1.3.1依赖导入
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
1.3.1swaggerConfig配置
package com.thxy.tygl.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableSwagger2 //开启swagger注解
public class SwaggerConfig {
@Bean //配置swagger Docker bean实例
public Docket docket(Environment environment){
/*
.slect().apis(RequestHandlerSelectors.basePackage("com.thxy.tygl.controller")) 扫描指定包下的接口
.path(PathSelectors.ant("/*")) 指定符合条件的url地址接口
*/
Profiles profile=Profiles.of("dev"); //创建开启swagger所需环境
boolean flag=environment.acceptsProfiles(profile); //检查环境是否是dev
// System.out.println(flag);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("silence") //配置分组信息
.enable(flag) //根据当前环境确定是否开启swagger
.apiInfo(apiInfo())
.select().apis(RequestHandlerSelectors.basePackage("com.thxy.tygl.controller")).paths(PathSelectors.any()).build()
;
}
//配置Docker的Apiinfo信息
public ApiInfo apiInfo(){
Contact contact=new Contact("slience","https://mp.csdn.net/console/article","1195279222@qq.com");//作者信息
return new ApiInfo("Silence Api Documentation", //首页标题
"tygl Api Documentation", //首页描述
"1.0", //版本
"https://mp.csdn.net/console/article", //团队服务器URL
contact ,
"apache2.0",
"",
new ArrayList());
}
}
启动项目访问:
http://localhost:8080/swagger-ui.html
更多swagger操作请浏览此网站
swagger更多参数