swagger ui是一個API在線文檔生成和測試的利器,目前發現最好用的。
為什么好用?支持API自動生成同步的在線文檔, 這些文檔可用于項目內部API審核,方便測試人員了解API,這些文檔可作為客戶產品文檔的一部分進行發布,支持API規范生成代碼,生成的客戶端和服務器端骨架代碼可以加速開發和測試速度.
不多說下面進行一下配置
首先引入pom 依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2.配置文件
@Configuration //必須存在
@EnableSwagger2 //必須存在
public class SwaggerConfig{
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.wen.security.controller"))
.paths(PathSelectors.any())
.build();
}
protected ApiInfo getApiInfo()
{
return new ApiInfo("Rest Web Service", "cxhc Rest Web Service " + new Date(), "", "",
new Contact("cxhc", "", ""), "", "",new ArrayList<VendorExtension>());
}
}
3.添加方法和參數描述
@GetMapping("/{id}")
@ApiOperation(value = "普通線程", notes = "普通線程描述")
public String testSwagger(@ApiParam(name = "id", value = "編號", required = true) @PathVariable int id) {
logger.info("主線程開始");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("主線程結束");
return "success";
}
展示效果如下
這里寫圖片描述
4.注解描述
@ApiIgnore
忽略暴露的 api
@ApiOperation(value = "查找", notes = "根據用戶 ID 查找用戶")
添加說明
@Api :用在類上,說明該類的作用
@ApiImplicitParams :用在方法上包含一組參數說明
@ApiResponses :用于表示一組響應
@ApiResponse :用在@ApiResponses 中,一般用于表達一個錯誤的響應信息
code:數字,例如 400
message:信息,例如"請求參數沒填好"
response:拋出異常的類
@ApiModel :描述一個 Model 的信息(這種一般用在 post 創建的時候,使用@RequestBody 這樣的場景,請求參數無法使用@ApiImplicitParam 注解進行描述的時候)
@ApiModelProperty :描述一個 model 的屬性
文章地址:http://www.haha174.top/article/details/254541
項目源碼:https://github.com/haha174/imooc-security.git
歡迎關注,更多福利
這里寫圖片描述