您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用SpringBoot項目配置Swagger接口api搭建REST”,在日常操作中,相信很多人在怎么用SpringBoot項目配置Swagger接口api搭建REST問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用SpringBoot項目配置Swagger接口api搭建REST”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
一、簡述
開發(fā)前后端分離架構(gòu)的項目,往往調(diào)試后端Web接口需要用到POSTMAN工具。雖然POSTMAN工具的功能非常強大,但是請求參數(shù)很多的情況下,我們手寫這些參數(shù)和數(shù)據(jù)還是非常麻煩的。因此我們需要一個調(diào)試后端Web接口更加簡便的方法。恰好Swagger提供了RESTAPI調(diào)用方式,我們不需要借助任何工具的情況下,訪問Swagger頁面,就可以對Web接口進行調(diào)用和調(diào)試,這種調(diào)試方式的效率要遠超POSTMAN軟件。
二、pom.xml中導(dǎo)入Swagger的依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
@Configuration @EnableSwagger2 public class SwaggerConfig { //java項目www.fhadmin.org @Bean public Docket createRestApi() { Docket docket = new Docket(DocumentationType.SWAGGER_2); // ApiInfoBuilder 用于在Swagger界面上添加各種信息 ApiInfoBuilder builder = new ApiInfoBuilder(); builder.title("XXXX系統(tǒng)"); ApiInfo apiInfo = builder.build(); docket.apiInfo(apiInfo); // ApiSelectorBuilder 用來設(shè)置哪些類中的方法會生成到REST API中 ApiSelectorBuilder selectorBuilder = docket.select(); selectorBuilder.paths(PathSelectors.any()); //所有包下的類 //使用@ApiOperation的方法會被提取到REST API中 selectorBuilder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)); docket = selectorBuilder.build(); /* * 下面的語句是開啟對JWT的支持,當(dāng)用戶用Swagger調(diào)用受JWT認證保護的方法, * 必須要先提交參數(shù)(例如令牌) */ //存儲用戶必須提交的參數(shù) List<ApiKey> apikey = new ArrayList(); //規(guī)定用戶需要輸入什么參數(shù) apikey.add(new ApiKey("token", "token", "header")); docket.securitySchemes(apikey); //如果用戶JWT認證通過,則在Swagger中全局有效 AuthorizationScope scope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] scopeArray = {scope}; //存儲令牌和作用域 SecurityReference reference = new SecurityReference("token", scopeArray); List refList = new ArrayList(); refList.add(reference); SecurityContext context = SecurityContext.builder().securityReferences(refList).build(); List cxtList = new ArrayList(); cxtList.add(context); docket.securityContexts(cxtList); return docket; } }
package com.gaoyang.emos.wx.controller; import com.gaoyang.emos.wx.common.util.ResponseResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; //java項目www.fhadmin.org @RestController @RequestMapping("/test") @Api("測試Web接口") public class TestController { @RequestMapping("testSwagger") @ApiOperation("測試Swagger配置") public ResponseResult testSwagger(){ return ResponseResult.error(200,"OK"); } }
五、訪問swagger-ui.html頁面
注意:還需要添加在啟動類中添加 @EnableSwagger2 注解,,否則會出線,頁面加載失敗情況!
啟動項目之后,訪問: http://localhost:8080/emos-wx-api/swagger-ui.html
到此,關(guān)于“怎么用SpringBoot項目配置Swagger接口api搭建REST”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。