1. 程式人生 > >SwaggerUI 自動生成文件

SwaggerUI 自動生成文件

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 的屬性

歡迎關注,更多福利

這裡寫圖片描述