Swagger類的配置

1.引入相關依賴

<!--swagger-->
<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.創建Swagger的配置類

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //過濾掉admin路徑下的所有頁面
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                //過濾掉所有error或error.*頁面
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

    @Bean
    public Docket adminApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只顯示admin路徑下的頁面
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("網站-課程中心API文檔")
                .description("本文檔描述了課程中心微服務接口定義")
                .version("1.0")
                .contact(new Contact("sun", "http://atguigu.com", "[email protected]"))
                .build();
    }

    private ApiInfo adminApiInfo(){

        return new ApiInfoBuilder()
                .title("後臺管理系統-課程中心API文檔")
                .description("本文檔描述了後臺管理系統課程中心微服務接口定義")
                .version("1.0")
                .contact(new Contact("sun", "http://atguigu.com", "[email protected]"))
                .build();
    }
}

3.Swagger註解說明

@Api(tags="")
用在請求的類上,表示對類的說明
tags"說明該類的作用,可以在UI界面上看到的註解"

@ApiOperation(value="")
用在請求的方法上,說明方法的用途、作用
value=“說明方法的用途、作用”

@ApiImplicitParams
用在請求的方法上,表示一組參數說明

@ApiImplicitParam
@ApiImplicitParam:指定一個請求參數的各個方面
value:參數的漢字說明、解釋
required:參數是否必須傳
paramType:參數放在哪個地方
header –> 請求頭的獲取:@·RequestHeader
query –> 請求參數的獲取:@RequestParam
path(用於restful接口)–> 請求路徑變量的獲取:

@PathVariable
body(不常用)
form(不常用)
dataType:參數類型,默認String,其它值dataType=“Integer”
defaultValue:參數的默認值

@ApiResponses
用在請求的方法上,表示一組響應

@ApiResponse
用在@ApiResponses中,一般用於表達一個錯誤的響應信息
code:數字,例如400
message:信息,例如"請求參數沒填好"
response:拋出異常的類

@ApiModel
主要有兩種用途:
用於響應類上,表示一個返回響應數據的信息
入參實體:使用@RequestBody這樣的場景, 請求參數無法使用@ApiImplicitParam註解進行描述的時候

@ApiModelProperty
用在屬性上,描述響應類的屬性

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章