一、Swagger2是什么楼入?
為了減少程序員撰寫文檔時(shí)間闲询,提高生產(chǎn)力, Swagger2 應(yīng)運(yùn)而生浅辙,使用 Swagger2 可以減少編寫過多的文檔,只需要通過代碼就能生成文檔API,提供給前端人員阎姥,非常方便记舆。
二、添加依賴
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger2 官方前端UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger2 優(yōu)化后的前端UI -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.5</version>
</dependency>
三呼巴、swagger2配置
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
/**
* @Author Allens
* @Date 2020-01-14 15:13
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
// http://localhost:8088/swagger-ui.html 官方前端UI頁面地址
// http://localhost:8088/doc.html 優(yōu)化過的前端UI頁面地址
//配置swagger2核心配置 docket
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) //指定api類型為swagger2
.apiInfo(apiInfo()) //用于定義api文檔匯總信息
.select().apis(RequestHandlerSelectors.basePackage("com.allens.controller")) //指定controller包
.paths(PathSelectors.any()) //所用controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("電商平臺(tái)接口api") //文檔頁標(biāo)題
.contact(new Contact("allens","http://www.baidu.com","123@qq.com")) //聯(lián)系人信息
.description("提供的api文檔") //詳細(xì)信息
.version("1.0.0") //文檔版本號(hào)
.termsOfServiceUrl("https://www.baidu.com") //網(wǎng)站地址
.build();
}
}
四泽腮、注解標(biāo)簽使用
@ApiIgnore
在swagger文檔中忽略掉該api@Api
在swagger中顯示該api的相關(guān)信息
示例:
/**
* tags:api標(biāo)題,如果添加了兩個(gè)標(biāo)題衣赶,這在swagger中會(huì)顯示成兩個(gè)分組诊赊,但包含的路由是同一組
*/
@Api(value = "登錄相關(guān)", tags = {"用于用于注冊登錄的相關(guān)接口"})
-
@ApiOperation
在swagger中顯示api下某個(gè)路由的相關(guān)信息
示例:
/**
* value:接口標(biāo)題
* notes:接口的備注
* response:響應(yīng)的類
* httpMethod:請求方式 值為:"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" 和 "PATCH"誓篱。
* responseContainer:數(shù)據(jù)類型 值為:"List", "Set" 或 "Map"唱蒸。
*/
@ApiOperation(value = "用戶注冊", notes = "用戶注冊~", response = Users.class, httpMethod = "POST", responseContainer = "Map")
-
@ApiModel
在swagger中對于傳參數(shù)據(jù)的注解
示例:
/**
* vale:數(shù)據(jù)類型
* description:參數(shù)說明
*/
@ApiModel(value = "用戶對象BO", description = "從客戶端,用用戶傳入的數(shù)據(jù)封裝在此entity中")
-
@ApiModelProperty
在swagger中顯示的傳參中的屬性的相關(guān)信息
示例:
/**
* value : 在swagger中對于字段的注釋
* name :在swagger中對于字段的重寫 不建議使用
* example :字段的默認(rèn)值
* required :是否必傳
*/
@ApiModelProperty(value = "密碼", name = "confirm_password", example = "123455", required = true)
-
@ApiParam
針對參數(shù)所作的行為做的一個(gè)解釋
示例:
public IMOOCJSONResult querySubCats(
/**
* name:參數(shù)名
* value:參數(shù)的作用的描述
* required:true或者false辽慕。表示是否必填
*/
@ApiParam(name = "rootCatId", value = "一級分類id", required = true)
@PathVariable Integer rootCatId) {
}