新聞中心
這篇文章主要講解了“Swagger接口說明文檔的配置和使用方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Swagger接口說明文檔的配置和使用方法”吧!
目前創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、梨樹網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
Swagger是一個(gè)開源的接口配置文檔,一般用于前后端分離,代替后端人員為前端人員書寫繁瑣的接口文檔,使后端人員從繁瑣的接口文檔中解脫出來。Swagger如何使用呢?首先我們要在springboot中的pom文件引入依賴包
io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0
其次,添加swagger的配置文件,該配置文件定義了網(wǎng)頁訪問swagger2的路徑以及標(biāo)題,描述等信息
package com.xash.quartzDemo.config; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 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; @Configurable public class SwaggerConfig{ @Bean public Docket api(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("測(cè)試模塊") .apiInfo(getApiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller")) .paths(PathSelectors.regex(".*/.*")) .build(); } private ApiInfo getApiInfo(){ return new ApiInfoBuilder() .title("測(cè)試模塊") .description("小標(biāo)題") .version("1.0") .build(); } @Bean public Docket api1(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("測(cè)試模塊1") .apiInfo(getApiInfo1()) .select() .apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller")) .paths(PathSelectors.regex(".*/.*")) .build(); } @Bean private ApiInfo getApiInfo1(){ return new ApiInfoBuilder() .title("測(cè)試模塊1") .description("小標(biāo)題") .version("1.0") .build(); } }
這樣,我們就可以在要添加接口文檔的地方利用注解添加相應(yīng)的接口文檔信息了
例如:
@Api(tags="這是個(gè)測(cè)試Controller")
public class PermissionController {}類上加注解,說明該類具有的功能
@ApiOperation(value = "根據(jù)用戶id查詢權(quán)限")
public String selectPermissionById(ModelMap map,@ApiParam("id") @RequestParam("id")int id){
System.out.println("開始查詢");
map.put("name", "歡飲使用thymeleaf模板引擎");
map.put("sysPermission", permissionService.selectPermissionById(id));
return "index";
}方法上加注解,表示方法的功能,以及可見在形參上加注解,指定形參,對(duì)形參進(jìn)行描述
訪問地址默認(rèn)為192.168.2.199:8080/項(xiàng)目應(yīng)用/swagger2-ui.html
感謝各位的閱讀,以上就是“Swagger接口說明文檔的配置和使用方法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Swagger接口說明文檔的配置和使用方法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
本文題目:Swagger接口說明文檔的配置和使用方法
文章轉(zhuǎn)載:http://www.dlmjj.cn/article/ppcjcg.html