Swagger的优点
1.号称世界上最流行的api框架
2.RestFull API文档在线自动生成工具,API文档与API定义同步更新
3.直接运行,可以测试API接口
4.支持多种语言
springboot集成Swagger
创建新的Springboot项目
相关Maven依赖jar包
<!--swagger核心jar包 swagger2 swagger-ui-->
<!-- 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>
核心jar包:Swgger2 和Swagger-ui
配置Swagger config
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
运行测试
localhost:8080/swagger-ui.html
该页面四大部分
源码
ApiInfo 配置Swagger信息
public class ApiInfo {
public static final Contact DEFAULT_CONTACT = new Contact("", "", "");
public static final ApiInfo DEFAULT;
private final String version;
private final String title;
private final String description;
private final String termsOfServiceUrl;
private final String license;
private final String licenseUrl;
private final Contact contact;
private final List<VendorExtension> vendorExtensions;
/** @deprecated */
@Deprecated
public ApiInfo(String title, String description, String version, String termsOfServiceUrl, String contactName, String license, String licenseUrl) {
this(title, description, version, termsOfServiceUrl, new Contact(contactName, "", ""), license, licenseUrl, new ArrayList());
}
public ApiInfo(String title, String description, String version, String termsOfServiceUrl, Contact contact, String license, String licenseUrl, Collection<VendorExtension> vendorExtensions) {
this.title = title;
this.description = description;
this.version = version;
this.termsOfServiceUrl = termsOfServiceUrl;
this.contact = contact;
this.license = license;
this.licenseUrl = licenseUrl;
this.vendorExtensions = Lists.newArrayList(vendorExtensions);
}
修改swagger-ui页面Swagger核心类ApiInfo
源码:
DEFAULT = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
复制修改
//配置Swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("TheSky", "https://www.csdn.net/", "xxx@qq.com");
return new ApiInfo("TheSky的SwaggerAPI文档",
"Api Documentation",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
得到