色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

SpringBoot集成swagger-ui以及swagger分組顯示操作

瀏覽:3日期:2023-04-20 14:27:56

大家好,這篇文章展示下如何在springboot項目中集成swagger-ui。有人說,這都是老生常談,網(wǎng)上的例子數(shù)不勝數(shù)。確實swagger誕生至今已經(jīng)很久了,但是在使用過程中我遇到一個問題,下面給大家分享下我的使用心得吧。

1.swagger配置類

第一步,需要在pom中引入相應的配置,這里使用2.7.0的版本。需要注意的是2.7.0和2.8.0的版本在界面風格上差異很大,如果感興趣,可以試試2.8.0以上的版本,我比較青睞使用2.7.0及以下的版本,因為界面比較清爽。

第一步 引入pom

<!--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>

第二步

在代碼中加入相應的配置,新建config包,寫入swaggerConfig配置類:

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.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class SwaggerConfig { /** * 創(chuàng)建API應用 * apiInfo() 增加API相關信息 * 通過select()函數(shù)返回一個ApiSelectorBuilder實例,用來控制哪些接口暴露給Swagger來展現(xiàn), * 本例采用指定掃描的包路徑來定義指定要建立API的目錄。 * * @return */ @Bean public Docket restApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName('標準接口') .apiInfo(apiInfo('Spring Boot中使用Swagger2構建RESTful APIs', '1.0')) .useDefaultResponseMessages(true) .forCodeGeneration(false) .select() .apis(RequestHandlerSelectors.basePackage('com.xqnode.learning.controller')) .paths(PathSelectors.any()) .build(); } /** * 創(chuàng)建該API的基本信息(這些基本信息會展現(xiàn)在文檔頁面中) * 訪問地址:http://ip:port/swagger-ui.html * * @return */ private ApiInfo apiInfo(String title, String version) { return new ApiInfoBuilder() .title(title) .description('更多請關注: https://jb51.net') .termsOfServiceUrl('https://jb51.net') .contact(new Contact('xqnode', 'https://jb51.net', 'xiaqingweb@163.com')) .version(version) .build(); }}

.apis(RequestHandlerSelectors.basePackage(“com.xqnode.learning.controller”))這個配置是用來指定我們的接口層的位置,大家可以根據(jù)你自己項目的實際情況來進行修改。.apiInfo()是定義一些我們項目的描述信息,可以根據(jù)實際需要在參數(shù)中修改。需要注意的是配置類的頭部需要加上@Configuration,聲明配置類,以及@EnableSwagger2加載swagger的一些相關配置。

2.使用swagger

我們在剛才指定的接口層使用swagger來說明接口的使用方法。

import com.fasterxml.jackson.databind.ObjectMapper;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.io.IOException;import java.util.Map;@RestController@RequestMapping('/api')@Api(tags = '標準演示接口')public class ApiController { @Resource private ObjectMapper mapper; @PostMapping('/ps') @ApiOperation(value = '接受json參數(shù)', notes = '演示json參數(shù)是否接受成功') public String post(@ApiParam(name = '接收json參數(shù)', defaultValue = '{}') @RequestBody String json) throws IOException { Map map = mapper.readValue(json, Map.class); System.out.println(map); return json; }}

然后我們啟動項目,打開http://ip:port/swagger-ui.html:

SpringBoot集成swagger-ui以及swagger分組顯示操作

不輸入任何參數(shù),點擊try it out!按鈕:

SpringBoot集成swagger-ui以及swagger分組顯示操作

從頁面上我們可以看到我們在接口的頭部指定的接口類描述(@Api),以及在接口方法上指定的方法描述(@ApiOperation),在接口參數(shù)上指定的參數(shù)描述(@ApiParam)都已經(jīng)生效,這都是基于swagger來實現(xiàn)的,但是需要注意的是swagger只能提供接口的描述信息。

3.額外的學習經(jīng)歷

我在使用swagger的時候,遇到一個需求是這樣的,我需要在兩個接口層都使用swagger,即將兩個接口層的api分組展示,例如下面這兩個接口層:

SpringBoot集成swagger-ui以及swagger分組顯示操作

我啟動項目后訪問swagger頁面,發(fā)現(xiàn)一個很奇怪的問題,就是other層的接口看不到:

SpringBoot集成swagger-ui以及swagger分組顯示操作

我猜測原因可能是我在配置類中指定的接口層位置影響了swagger api的顯示。于是我百度了一下,找到一個解決方案,就是不指定接口層的位置,而指定注解的@RestController

@Bean public Docket restApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName('standard') .apiInfo(apiInfo('Spring Boot中使用Swagger2構建RESTful APIs', '1.0')) .useDefaultResponseMessages(true) .forCodeGeneration(false) .select()// .apis(RequestHandlerSelectors.basePackage('com.xqnode.learning.controller')) .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); }

swagger界面中出現(xiàn)了另一個接口的api:

SpringBoot集成swagger-ui以及swagger分組顯示操作

但是這樣的效果并不好。大家試想一下,我們?yōu)槭裁匆獙涌诜謱幽兀坎痪褪菫榱藢I(yè)務隔離么,這樣在一個界面中出現(xiàn)兩個接口層的api,對于我們查找接口非常的不方便,也打亂了我們對接口分層的目的。那么怎么才能將其進行隔離開呢?

其實很簡單,我們只需要重新定義一個Docket的bean,在其中指定另外接口層的位置即可:

@Bean public Docket restApi2() { return new Docket(DocumentationType.SWAGGER_2) .groupName('其他接口') .apiInfo(apiInfo('Other APIs', '2.0')) .select() .apis(RequestHandlerSelectors.basePackage('com.xqnode.learning.other')) .paths(PathSelectors.regex('/other.*')) .build(); }

我們在這里指定了第二個接口層的位置,同時指定了它的路徑前綴,這樣我們在swagger頁面中就能很方便很清晰的找到它里面的接口了。

接口層1:標準接口

SpringBoot集成swagger-ui以及swagger分組顯示操作

接口層2:其他接口

SpringBoot集成swagger-ui以及swagger分組顯示操作

現(xiàn)在我們只要通過切換分組,就可以找到我們關注的接口層的api了。

下面貼出完整的配置類:

@Configuration@EnableSwagger2public class SwaggerConfig { /** * 創(chuàng)建API應用 * apiInfo() 增加API相關信息 * 通過select()函數(shù)返回一個ApiSelectorBuilder實例,用來控制哪些接口暴露給Swagger來展現(xiàn), * 本例采用指定掃描的包路徑來定義指定要建立API的目錄。 * * @return */ @Bean public Docket restApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName('standard') .apiInfo(apiInfo('Spring Boot中使用Swagger2構建RESTful APIs', '1.0')) .useDefaultResponseMessages(true) .forCodeGeneration(false) .select() .apis(RequestHandlerSelectors.basePackage('com.xqnode.learning.controller'))// .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.regex('/api.*')) .build(); } @Bean public Docket restApi2() { return new Docket(DocumentationType.SWAGGER_2) .groupName('其他接口') .apiInfo(apiInfo('Other APIs', '2.0')) .select() .apis(RequestHandlerSelectors.basePackage('com.xqnode.learning.other')) .paths(PathSelectors.regex('/other.*')) .build(); } /** * 創(chuàng)建該API的基本信息(這些基本信息會展現(xiàn)在文檔頁面中) * 訪問地址:http://ip:port/swagger-ui.html * * @return */ private ApiInfo apiInfo(String title, String version) { return new ApiInfoBuilder() .title(title) .description('更多請關注: https://jb51.net') .termsOfServiceUrl('https://jb51.net') .contact(new Contact('xqnode', 'https://jb51.net', 'xiaqingweb@163.com')) .version(version) .build(); }}

至此,springboot集成swagger2完成,同時加了一個餐,還滿意吧?哈哈

以上這篇SpringBoot集成swagger-ui以及swagger分組顯示操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關文章:
主站蜘蛛池模板: 久久精品亚瑟全部免费观看 | 日韩一区二区三区在线免费观看 | 99国产精品视频免费观看 | a级毛片毛片免费观看永久 a级毛片毛片免费很很综合 | 一区二区网站 | 免费特黄一级欧美大片 | 国内精品成人女用 | 日韩99在线 | 国产精品视_精品国产免费 国产精品视频久 | 久久99精品久久久久久久野外 | 欧美三级观看 | 国产一区二区三区不卡在线观看 | 免费一级毛片在线播放放视频 | 国产高清自拍视频 | a级国产乱理伦片在线观看99 | 成人国产一区 | 国产成人精品自拍 | 国产午夜精品久久久久小说 | www.99精品 | 精品国产v无码大片在线观看 | 欧美成人精品一区二区 | 国产一级一片 | 精品日韩欧美一区二区三区 | 亚洲天堂网在线视频 | 国产l精品国产亚洲区久久 国产tv在线 | 欧美日韩国产亚洲一区二区 | 国产精品特黄一级国产大片 | 久草视频精品 | 久久国产成人亚洲精品影院老金 | 深夜福利视频在线观看免费播放 | 国产成人久久综合二区 | 日韩一区二区三区在线观看 | 久久久精品一区 | 久久国产视频一区 | 男女无遮挡拍拍拍免费1000 | 免费无遮挡毛片 | 91精品国产91久久久久久 | 国产成人综合精品 | 欧美久久久久久 | 在线视频一区二区三区四区 | 欧美人成在线观看网站高清 |