swagger2和knife4j的詳細(xì)使用教程(入門級(jí))_第1頁(yè)
swagger2和knife4j的詳細(xì)使用教程(入門級(jí))_第2頁(yè)
swagger2和knife4j的詳細(xì)使用教程(入門級(jí))_第3頁(yè)
swagger2和knife4j的詳細(xì)使用教程(入門級(jí))_第4頁(yè)
swagger2和knife4j的詳細(xì)使用教程(入門級(jí))_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第swagger2和knife4j的詳細(xì)使用教程(入門級(jí))目錄1、swagger介紹2、swagger常用注解3、swagger入門案例4、knife4j介紹5、knife4j入門案例總結(jié)入門級(jí)swagger2和knife4j的詳細(xì)使用

重要的使事情說(shuō)三遍

重要的使事情說(shuō)三遍

重要的使事情說(shuō)三遍

使用swagge或knife4j都有與springboot的版本兼容問(wèn)題,該文章使用的springboot版本是2.2.2.RELEASE版本,如果高版本可能會(huì)出現(xiàn)啟動(dòng)報(bào)錯(cuò)

1、swagger介紹

相信無(wú)論是前端還是后端開(kāi)發(fā),都或多或少地被接口文檔折磨過(guò)。前端經(jīng)常抱怨后端給的接口文檔與實(shí)際情況不一致。后端又覺(jué)得編寫及維護(hù)接口文檔會(huì)耗費(fèi)不少精力,經(jīng)常來(lái)不及更新。其實(shí)無(wú)論是前端調(diào)用后端,還是后端調(diào)用后端,都期望有一個(gè)好的接口文檔。但是這個(gè)接口文檔對(duì)于程序員來(lái)說(shuō),就跟注釋一樣,經(jīng)常會(huì)抱怨別人寫的代碼沒(méi)有寫注釋,然而自己寫起代碼起來(lái),最討厭的,也是寫注釋。所以僅僅只通過(guò)強(qiáng)制來(lái)規(guī)范大家是不夠的,隨著時(shí)間推移,版本迭代,接口文檔往往很容易就跟不上代碼了。

使用Swagger你只需要按照它的規(guī)范去定義接口及接口相關(guān)的信息。再通過(guò)Swagger衍生出來(lái)的一系列項(xiàng)目和工具,就可以做到生成各種格式的接口文檔,生成多種語(yǔ)言的客戶端和服務(wù)端的代碼,以及在線接口調(diào)試頁(yè)面等等。這樣,如果按照新的開(kāi)發(fā)模式,在開(kāi)發(fā)新版本或者迭代版本的時(shí)候,只需要更新Swagger描述文件,就可以自動(dòng)生成接口文檔和客戶端服務(wù)端代碼,做到調(diào)用端代碼、服務(wù)端代碼以及接口文檔的一致性。

為了簡(jiǎn)化swagger的使用,Spring框架對(duì)swagger進(jìn)行了整合,建立了Spring-swagger項(xiàng)目,后面改成了現(xiàn)在的Springfox。通過(guò)在項(xiàng)目中引入Springfox,可以掃描相關(guān)的代碼,生成描述文件,進(jìn)而生成與代碼一致的接口文檔和客戶端代碼。

Springfox對(duì)應(yīng)的maven坐標(biāo)如下:

注意:swagger與springBoot的版本是有兼用性的,需要對(duì)應(yīng)的版本號(hào)

dependency

groupIdio.springfox/groupId

artifactIdspringfox-swagger-ui/artifactId

version2.9.2/version

/dependency

dependency

groupIdio.springfox/groupId

artifactIdspringfox-swagger2/artifactId

version2.9.2/version

/dependency

2、swagger常用注解

注解說(shuō)明@Api用在請(qǐng)求的類上,例如Controller,表示對(duì)類的說(shuō)明@ApiModel用在類上,通常是實(shí)體類,表示一個(gè)返回響應(yīng)數(shù)據(jù)的信息@ApiModelProperty用在屬性上,描述響應(yīng)類的屬性@ApiOperation用在請(qǐng)求的方法上,說(shuō)明方法的用途、作用@ApiImplicitParams用在請(qǐng)求的方法上,表示一組參數(shù)說(shuō)明@ApiImplicitParam用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面

3、swagger入門案例

3.1、第一步:創(chuàng)建maven工程swagger_demo并配置pom.xml文件

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

modelVersion4.0.0/modelVersion

parent

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-parent/artifactId

version2.2.2.RELEASE/version

relativePath/

/parent

groupIdorg.example/groupId

artifactIdswagger_demo/artifactId

version1.0-SNAPSHOT/version

properties

piler.source8/piler.source

piler.target8/piler.target

/properties

dependencies

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

dependency

groupIdio.springfox/groupId

artifactIdspringfox-swagger-ui/artifactId

version2.9.2/version

/dependency

dependency

groupIdio.springfox/groupId

artifactIdspringfox-swagger2/artifactId

version2.9.2/version

/dependency

dependency

groupIdjectlombok/groupId

artifactIdlombok/artifactId

/dependency

/dependencies

/project

3.2、創(chuàng)建application.yml文件

server:

port:9000

3.3、第三步:創(chuàng)建實(shí)體類User和Menu

packagecom.zcl.entity;

importio.swagger.annotations.ApiModel;

importio.swagger.annotations.ApiModelProperty;

importlombok.Data;

*項(xiàng)目名稱:swagger_demo

*描述:用戶實(shí)體類

*@authorzhong

*@date2025-08-296:50

@Data

@ApiModel(value="用戶實(shí)體",description="用戶響應(yīng)實(shí)體")

publicclassUser{

@ApiModelProperty(value="主鍵")

privateintid;

@ApiModelProperty(value="姓名")

privateStringname;

@ApiModelProperty(value="年齡")

privateintage;

@ApiModelProperty(value="地址")

privateStringaddress;

packagecom.zcl.entity;

importio.swagger.annotations.ApiModel;

importio.swagger.annotations.ApiModelProperty;

importlombok.Data;

*項(xiàng)目名稱:swagger_demo

*描述:菜單實(shí)體類

*@authorzhong

*@date2025-08-296:49

@Data

@ApiModel(value="菜單實(shí)體",discriminator="菜單實(shí)體描述")

publicclassMenu{

@ApiModelProperty(value="主鍵")

privateintid;

@ApiModelProperty(value="菜單名稱")

privateStringname;

3.4、第四步:創(chuàng)建UserController和MenuController

packagecom.zcl.controller.user;

importcom.zcl.entity.User;

importio.swagger.annotations.Api;

importio.swagger.annotations.ApiImplicitParam;

importio.swagger.annotations.ApiImplicitParams;

importio.swagger.annotations.ApiOperation;

importorg.springframework.web.bind.annotation.*;

importjava.util.ArrayList;

importjava.util.List;

*項(xiàng)目名稱:swagger_demo

*描述:用戶控制器

*@authorzhong

*@date2025-08-296:55

@Api(value="用戶控制器")

@RestController

@RequestMapping("/user")

publicclassUserController{

@GetMapping("/getUsers")

@ApiOperation(value="查詢所有用戶",notes="查詢所有用戶信息")

publicListUsergetAllUsers(){

Useruser=newUser();

user.setId(100);

user.setName("itcast");

user.setAge(20);

user.setAddress("bj");

ListUserlist=newArrayList();

list.add(user);

returnlist;

@PostMapping("/save")

@ApiOperation(value="新增用戶",notes="新增用戶信息")

publicStringsave(@RequestBodyUseruser){

return"OK";

@PutMapping("/update")

@ApiOperation(value="修改用戶",notes="修改用戶信息")

publicStringupdate(@RequestBodyUseruser){

return"OK";

@DeleteMapping("/delete")

@ApiOperation(value="刪除用戶",notes="刪除用戶信息")

publicStringdelete(intid){

return"OK";

@ApiImplicitParams({

@ApiImplicitParam(name="pageNum",value="頁(yè)碼",

required=true,type="Integer"),

@ApiImplicitParam(name="pageSize",value="每頁(yè)條數(shù)",

required=true,type="Integer"),

@ApiOperation(value="分頁(yè)查詢用戶信息")

@GetMapping(value="page/{pageNum}/{pageSize}")

publicStringfindByPage(@PathVariableIntegerpageNum,

@PathVariableIntegerpageSize){

return"OK";

packagecom.zcl.controller.menu;

importcom.zcl.entity.Menu;

importio.swagger.annotations.Api;

importio.swagger.annotations.ApiImplicitParam;

importio.swagger.annotations.ApiImplicitParams;

importio.swagger.annotations.ApiOperation;

importorg.springframework.web.bind.annotation.*;

importjava.util.ArrayList;

importjava.util.List;

*項(xiàng)目名稱:swagger_demo

*描述:菜單控制器

*@authorzhong

*@date2025-08-297:00

@Api(tags="菜單控制器")

@RestController

@RequestMapping("/menu")

publicclassMenuController{

@GetMapping("/getMenus")

@ApiOperation(value="查詢所有菜單",notes="查詢所有菜單信息")

publicListMenugetMenus(){

Menumenu=newMenu();

menu.setId(100);

menu.setName("itcast");

ListMenulist=newArrayList();

list.add(menu);

returnlist;

@PostMapping("/save")

@ApiOperation(value="新增菜單",notes="新增菜單信息")

publicStringsave(@RequestBodyMenumenu){

return"OK";

@PutMapping("/update")

@ApiOperation(value="修改菜單",notes="修改菜單信息")

publicStringupdate(@RequestBodyMenumenu){

return"OK";

@DeleteMapping("/delete")

@ApiOperation(value="刪除菜單",notes="刪除菜單信息")

publicStringdelete(intid){

return"OK";

@ApiImplicitParams({

@ApiImplicitParam(name="pageNum",value="頁(yè)碼",

required=true,type="Integer"),

@ApiImplicitParam(name="pageSize",value="每頁(yè)條數(shù)",

required=true,type="Integer"),

@ApiOperation(value="分頁(yè)查詢菜單信息")

@GetMapping(value="page/{pageNum}/{pageSize}")

publicStringfindByPage(@PathVariableIntegerpageNum,

@PathVariableIntegerpageSize){

return"OK";

3.5、第五步:創(chuàng)建配置類SwaggerAutoConfiguration

packagecom.zcl.config;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importspringfox.documentation.builders.ApiInfoBuilder;

importspringfox.documentation.builders.RequestHandlerSelectors;

importspringfox.documentation.service.ApiInfo;

importspringfox.documentation.service.Contact;

importspringfox.documentation.spi.DocumentationType;

importspringfox.documentation.spring.web.plugins.Docket;

importspringfox.documentation.swagger2.annotations.EnableSwagger2;

*項(xiàng)目名稱:swagger_demo

*描述:swagger自動(dòng)配置類

*@authorzhong

*@date2025-08-297:04

@Configuration

@EnableSwagger2

publicclassSwaggerAutoConfiguration{

@Bean

publicDocketcreateRestApi1(){

Docketdocket=newDocket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo()).groupName("用戶接口組")

.select()

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

.apis(RequestHandlerSelectors.basePackage("com.zcl.controller.user"))

.build();

returndocket;

@Bean

publicDocketcreateRestApi2(){

Docketdocket=newDocket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo()).groupName("菜單接口組")

.select()

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

.apis(RequestHandlerSelectors.basePackage("com.zcl.controller.menu"))

.build();

returndocket;

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

privateApiInfoapiInfo(){

returnnewApiInfoBuilder()

//頁(yè)面標(biāo)題

.title("API接口文檔")

//創(chuàng)建人

.contact(newContact("雕刻筆記","http://xiaozhong01.top",""))

//版本號(hào)

.version("1.0")

//描述

.description("API描述")

.build();

注意:如果不需要進(jìn)行分組,可以將后面的createRestApi2()進(jìn)行刪除就剩余一個(gè)組了,并將.groupName(用戶接口組)分組描述去掉以及將掃描控制器包修改即可

3.6、第六步:創(chuàng)建啟動(dòng)類SwaggerApplication

packagecom.zcl;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

*項(xiàng)目名稱:swagger_demo

*描述:swagger項(xiàng)目啟動(dòng)類

*@authorzhong

*@date2025-08-297:13

@SpringBootApplication

publicclassSwaggerApplication{

publicstaticvoidmain(String[]args){

SpringApplication.run(SwaggerApplication.class,args);

3.7、項(xiàng)目啟動(dòng)

執(zhí)行啟動(dòng)類main方法啟動(dòng)項(xiàng)目,訪問(wèn)地址:http://localhost:9000/swagger-ui.html

4、knife4j介紹

knife4j是為JavaMVC框架集成Swagger生成Api文檔的增強(qiáng)解決方案,前身是swagger-bootstrap-ui,取名knife4j是希望它能像一把匕首一樣小巧,輕量,并且功能強(qiáng)悍!其底層是對(duì)Springfox的封裝,使用方式也和Springfox一致,只是對(duì)接口文檔UI進(jìn)行了優(yōu)化。

核心功能:

文檔說(shuō)明:根據(jù)Swagger的規(guī)范說(shuō)明,詳細(xì)列出接口文檔的說(shuō)明,包括接口地址、類型、請(qǐng)求示例、請(qǐng)求參數(shù)、響應(yīng)示例、響應(yīng)參數(shù)、響應(yīng)碼等信息,對(duì)該接口的使用情況一目了然。在線調(diào)試:提供在線接口聯(lián)調(diào)的強(qiáng)大功能,自動(dòng)解析當(dāng)前接口參數(shù),同時(shí)包含表單驗(yàn)證,調(diào)用參數(shù)可返回接口響應(yīng)內(nèi)容、headers、響應(yīng)時(shí)間、響應(yīng)狀態(tài)碼等信息,幫助開(kāi)發(fā)者在線調(diào)試。

5、knife4j入門案例

第一步:創(chuàng)建maven工程knife4j_demo并配置pom.xml文件

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

modelVersion4.0.0/modelVersion

parent

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-parent/artifactId

version2.2.2.RELEASE/version

relativePath/

/parent

groupIdorg.example/groupId

artifactIdknife4j/artifactId

version1.0-SNAPSHOT/version

properties

piler.source8/piler.source

piler.target8/piler.target

/properties

dependencies

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

dependency

groupIdcom.github.xiaoymin/groupId

artifactIdknife4j-spring-boot-starter/artifactId

version2.0.1/version

/dependency

dependency

groupIdjectlombok/groupId

artifactIdlombok/artifactId

/dependency

/dependencies

/project

第二步:創(chuàng)建實(shí)體類User和Menu

packagecom.zcl.entity;

importio.swagger.annotations.ApiModel;

importio.swagger.annotations.ApiModelProperty;

importlombok.Data;

*項(xiàng)目名稱:swagger_demo

*描述:菜單實(shí)體類

*@authorzhong

*@date2025-08-296:49

@Data

@ApiModel(value="菜單實(shí)體",discriminator="菜單實(shí)體描述")

publicclassMenu{

@ApiModelProperty(value="主鍵")

privateintid;

@ApiModelProperty(value="菜單名稱")

privateStringname;

packagecom.zcl.entity;

importio.swagger.annotations.ApiModel;

importio.swagger.annotations.ApiModelProperty;

importlombok.Data;

*項(xiàng)目名稱:swagger_demo

*描述:用戶實(shí)體類

*@authorzhong

*@date2025-08-296:50

@Data

@ApiModel(value="用戶實(shí)體",description="用戶響應(yīng)實(shí)體")

publicclassUser{

@ApiModelProperty(value="主鍵")

privateintid;

@ApiModelProperty(value="姓名")

privateStringname;

@ApiModelProperty(value="年齡")

privateintage;

@ApiModelProperty(value="地址")

privateStringaddress;

第三步:創(chuàng)建UserController和MenuController

控制器與入門的swagger2是一樣的,直接復(fù)制下來(lái)

第四步:創(chuàng)建接口文檔配置屬性類SwaggerProperties

由于上一個(gè)swagger_demo的接口文檔配置文件是寫死在項(xiàng)目代碼中的,不利于后面的開(kāi)發(fā)與維護(hù),使用knife4j時(shí)并將相關(guān)的屬性配置以yml全局的方式靈活的進(jìn)行配置,方便進(jìn)行修改

packagecom.zcl.config;

importlombok.Data;

importperties.ConfigurationProperties;

importjava.util.ArrayList;

importjava.util.LinkedHashMap;

importjava.util.List;

importjava.util.Map;

*項(xiàng)目名稱:knife4j

*描述:配置屬性類,用于封裝yml配置文件中關(guān)于接口相關(guān)的配置文件

*@authorzhong

*@date2025-08-297:58

@Data

@ConfigurationProperties(prefix="knife.swagger")

publicclassSwaggerProperties{

/**標(biāo)題*/

privateStringtitle="在線文檔";

/**自定義組名*/

privateStringgroup="";

/**描述*/

privateStringdescription="在線文檔";

/**版本*/

privateStringversion="1.0";

/**聯(lián)系人*/

privateContactcontact=newContact();

/**swagger會(huì)解析的包路徑*/

privateStringbasePackage="com.zcl";

/**swagger會(huì)解析的url規(guī)則*/

privateListStringbasePath=newArrayList();

/**在basePath基礎(chǔ)上需要排除的url規(guī)則*/

privateListStringexcludePath=newArrayList();

/**分組文檔*/

privateMapString,DocketInfodocket=newLinkedHashMap();

publicStringgetGroup(){

if(group==null||"".equals(group)){

returntitle;

returngroup;

@Data

publicstaticclassDocketInfo{

/**標(biāo)題*/

privateStringtitle="在線文檔";

/**自定義組名*/

privateStringgroup="";

/**描述*/

privateStringdescription="在線文檔";

/**版本*/

privateStringversion="1.0";

/**聯(lián)系人*/

privateContactcontact=newContact();

/**swagger會(huì)解析的包路徑*/

privateStringbasePackage="com.zcl";

/**swagger會(huì)解析的url規(guī)則*/

privateListStringbasePath=newArrayList();

/**在basePath基礎(chǔ)上需要排除的url規(guī)則*/

privateListStringexcludePath=newArrayList();

/**分組文檔*/

privateMapString,DocketInfodocket=newLinkedHashMap();

publicStringgetGroup(){

if(group==null||"".equals(group)){

returntitle;

returngroup;

@Data

publicstaticclassContact{

/**聯(lián)系人*/

privateStringname="pinda";

/**聯(lián)系人url*/

privateStringurl="";

/**聯(lián)系人email*/

privateStringemail="";

第五步:創(chuàng)建application.yml文件

該配置文件的屬性是對(duì)應(yīng)上面的配置文件屬性的如果不對(duì)上就不會(huì)被讀取到

下面重點(diǎn)的是對(duì)【docket】進(jìn)行map分組的屬性配置

privateMapString,DocketInfodocket=newLinkedHashMap();

不進(jìn)行分組的配置

server:

port:7788

knife:

swagger:

enabled:true#是否啟用swagger的開(kāi)關(guān)

title:knife4j測(cè)試文檔

description:knife4j測(cè)試文檔描述

version:v1.0.0

basePackage:com.zcl.controller

進(jìn)行分組的配置

server:

port:7788

knife:

swagger:

enabled:true#是否啟用swagger的開(kāi)關(guān)

title:knife4j測(cè)試文檔

description:knife4j測(cè)試文檔描述

version:v1.0.0

docket:#進(jìn)行分組

user:#key

#value

title:用戶模塊

base-package:com.zcl.controller.user

menu:

title:菜單模塊

base-package:com.zcl.controller.menu

注意:上面配置文件中的enabled屬性是對(duì)應(yīng)著下面配置類中【@ConditionalOnProperty】通過(guò)注解判斷是否符合條件才讓配置類生效

第六步:創(chuàng)建配置類SwaggerAutoConfiguration

該配置類需要使到上面創(chuàng)建的SwaggerProperties生效以及需要啟用swagger注解

packagecom.zcl.config;

importmon.base.Predicate;

importmon.base.Predicates;

importorg.springframework.beans.BeansException;

importorg.springframework.beans.factory.BeanFactory;

importorg.springframework.beans.factory.BeanFactoryAware;

importorg.springframework.beans.factory.config.ConfigurableBeanFactory;

importorg.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

importperties.EnableConfigurationProperties;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importspringfox.documentation.builders.ApiInfoBuilder;

importspringfox.documentation.builders.PathSelectors;

importspringfox.documentation.builders.RequestHandlerSelectors;

importspringfox.documentation.service.ApiInfo;

importspringfox.documentation.service.Contact;

importspringfox.documentation.spi.DocumentationType;

importspringfox.documentation.spring.web.plugins.Docket;

importspringfox.documentation.swagger2.annotations.EnableSwagger2;

importjavax.annotation.Resource;

importjava.util.ArrayList;

importjava.util.List;

importjava.util.Map;

importjava.util.Set;

*項(xiàng)目名稱:knife4j

*描述:配置類

*@authorzhong

*@date2025-08-298:22

@Configuration

@EnableConfigurationProperties(SwaggerProperties.class)

@EnableSwagger2

@ConditionalOnProperty(name="knife.swagger.enabled",havingValue="true",

matchIfMissing=true)

publicclassSwaggerAutoConfigurationimplementsBeanFactoryAware{

privateBeanFactorybeanFactory;

*通過(guò)類型注入去查找beanFactory

*@parambeanFactory

*@throwsBeansException

@Override

publicvoidsetBeanFactory(BeanFactorybeanFactory)throwsBeansException{

this.beanFactory=beanFactory;

*注入自動(dòng)配置屬性類

@Resource

privateSwaggerPropertiesswaggerProperties;

*遍歷swaggerProperties屬性配置的內(nèi)容,轉(zhuǎn)換成List集合【方便判斷有沒(méi)有進(jìn)行分組,沒(méi)有分組就一個(gè)list,分組就有多個(gè)】

*使用@ConditionalOnProperty條件注入,bean工程有就不創(chuàng)建

*@return

@Bean

@ConditionalOnMissingBean

publicListDocketcreateRestApi(){

//強(qiáng)轉(zhuǎn)beanFactory為可配置的bean工廠

ConfigurableBeanFactoryconfigurableBeanFactory=

(ConfigurableBeanFactory)beanFactory;

//創(chuàng)建返回的list集合

ListDocketdocketList=newArrayList();

//獲取到分組的對(duì)象如果為空就是不分組

if(swaggerProperties.getDocket().isEmpty()){

Docketdocket=createDocket(swaggerProperties);

docketList.add(docket);

}else{

//存在分組對(duì)象

//獲取到key【user\menu】

SetStringkeySet=swaggerProperties.getDocket().keySet();

//遍歷key獲取到對(duì)應(yīng)的value

for(Stringkey:keySet){

//獲取到info

SwaggerProperties.DocketInfodocketInfo=swaggerProperties.getDocket().get(key);

ApiInfoapiInfo=newApiInfoBuilder()

//頁(yè)面標(biāo)題

.title(docketInfo.getTitle())

//創(chuàng)建人

.contact(newContact(docketInfo.getContact().getName(),

docketInfo.getContact().getUrl(),

docketInfo.getContact().getEmail()))

//版本號(hào)

.version(docketInfo.getVersion())

//描述

.description(docketInfo.getDescription())

.build();

//base-path處理

//當(dāng)沒(méi)有配置任何path的時(shí)候,解析/**

if(docketInfo.getBasePath().isEmpty()){

docketInfo.getBasePath().add("/**");

ListPredicateStringbasePath=newArrayList();

for(Stringpath:docketInfo.getBasePath()){

basePath.add(PathSelectors.ant(path));

//exclude-path處理

ListPredicateStringexcludePath=newArrayList();

for(Stringpath:docketInfo.getExcludePath()){

excludePath.add(PathSelectors.ant(path));

Docketdocket=newDocket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo)

.groupName(docketInfo.getGroup())

.select()

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

.apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage()))

.paths(Predicates.and(Predicates.not(Predicates.or(excludePath)),Predicates.or(basePath)))

.build();

configurableBeanFactory.registerSingleton(key,docket);

docket

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論