SpringBoot集成Swagger

pom.xml依賴

<!-- swagger -->

<dependency>

????<groupId>io.springfox</groupId>

????<artifactId>springfox-swagger2</artifactId>

????<version>2.5.0</version>

</dependency>

<!-- swagger-ui -->

<dependency>

????<groupId>io.springfox</groupId>

????<artifactId>springfox-swagger-ui</artifactId>

? ? <version>2.5.0</version>

</dependency>


配置文件

package com.xxx.wallet.article.web.util.swagger;

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.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

? ? public DocketcreateRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

//為當(dāng)前包路徑

? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage("com.xxx.wallet.article.web.controller"))

.paths(PathSelectors.any())

.build();

? ? }

//構(gòu)建 api文檔的詳細(xì)信息函數(shù)

? ? private ApiInfoapiInfo() {

return new ApiInfoBuilder()

//頁面標(biāo)題

? ? ? ? ? ? ? ? .title("WEB——文章頭圖接口管理")

//創(chuàng)建人

//? ? ? ? ? ? ? ? .contact(new Contact("admin", "null", "null"))

? ? ? ? ? ? ? ? //版本號

? ? ? ? ? ? ? ? .version("1.0")

//描述

? ? ? ? ? ? ? ? .description("restful API")

.build();

? ? }

}


訪問路徑:項目路勁+swagger-ui.html

例如:http://localhost:8888/dyj/swagger-ui.html


Swagger常用注解

- @Api()用于類;

表示標(biāo)識這個類是swagger的資源

- @ApiOperation()用于方法;

表示一個http請求的操作

- @ApiParam()用于方法,參數(shù),字段說明;

表示對參數(shù)的添加元數(shù)據(jù)(說明或是否必填等)

- @ApiModel()用于類

表示對類進(jìn)行說明,用于參數(shù)用實體類接收

- @ApiModelProperty()用于方法,字段

表示對model屬性的說明或者數(shù)據(jù)操作更改

- @ApiIgnore()用于類,方法,方法參數(shù)

表示這個方法或者類被忽略

- @ApiImplicitParam() 用于方法

表示單獨(dú)的請求參數(shù)

- @ApiImplicitParams() 用于方法,包含多個@ApiImplicitParam


@Api()

用于類;表示標(biāo)識這個類是swagger的資源

tags–表示說明

value–也是說明,可以使用tags替代

但是tags如果有多個值,會生成多個list


@ApiOperation() 用于方法;表示一個http請求的操作

value用于方法描述

notes用于提示內(nèi)容

tags可以重新分組(視情況而用)


@ApiParam() 用于方法,參數(shù),字段說明;表示對參數(shù)的添加元數(shù)據(jù)(說明或是否必填等)

name–參數(shù)名

value–參數(shù)說明

required–是否必填


@ApiModel()用于類;表示對類進(jìn)行說明,用于參數(shù)用實體類接收

value–表示對象名

description–描述

都可省略


@ApiModelProperty()用于方法,字段;表示對model屬性的說明或者數(shù)據(jù)操作更改

value–字段說明

name–重寫屬性名字

dataType–重寫屬性類型

required–是否必填

example–舉例說明

hidden–隱藏


@ApiIgnore()用于類或者方法上,可以不被swagger顯示在頁面上

比較簡單, 這里不做舉例


@ApiImplicitParam() 用于方法

表示單獨(dú)的請求參數(shù)

@ApiImplicitParams() 用于方法,包含多個@ApiImplicitParam

name–參數(shù)ming

value–參數(shù)說明

dataType–數(shù)據(jù)類型

paramType–參數(shù)類型

example–舉例說明

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容