大家好,這篇文章展示下如何在springboot項目中集成swagger-ui。有人說,這都是老生常談,網上的例子數不勝數。確實swagger誕生至今已經很久了,但是在使用過程中我遇到一個問題,下面給大家分享下我的使用心得吧。
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 @EnableSwagger2 public class SwaggerConfig { /** * 創建API應用 * apiInfo() 增加API相關信息 * 通過select()函數返回一個ApiSelectorBuilder實例,用來控制哪些接口暴露給Swagger來展現, * 本例采用指定掃描的包路徑來定義指定要建立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(); } /** * 創建該API的基本信息(這些基本信息會展現在文檔頁面中) * 訪問地址:http://ip:port/swagger-ui.html * * @return */ private ApiInfo apiInfo(String title, String version) { return new ApiInfoBuilder() .title(title) .description("更多請關注: http://www.ythuaji.com.cn") .termsOfServiceUrl("http://www.ythuaji.com.cn") .contact(new Contact("xqnode", "http://www.ythuaji.com.cn", "[email protected]")) .version(version) .build(); } }
.apis(RequestHandlerSelectors.basePackage(“com.xqnode.learning.controller”))這個配置是用來指定我們的接口層的位置,大家可以根據你自己項目的實際情況來進行修改。.apiInfo()是定義一些我們項目的描述信息,可以根據實際需要在參數中修改。需要注意的是配置類的頭部需要加上@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參數", notes = "演示json參數是否接受成功") public String post(@ApiParam(name = "接收json參數", 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:
不輸入任何參數,點擊try it out!按鈕:
從頁面上我們可以看到我們在接口的頭部指定的接口類描述(@Api),以及在接口方法上指定的方法描述(@ApiOperation),在接口參數上指定的參數描述(@ApiParam)都已經生效,這都是基于swagger來實現的,但是需要注意的是swagger只能提供接口的描述信息。
3.額外的學習經歷
我在使用swagger的時候,遇到一個需求是這樣的,我需要在兩個接口層都使用swagger,即將兩個接口層的api分組展示,例如下面這兩個接口層:
我啟動項目后訪問swagger頁面,發現一個很奇怪的問題,就是other層的接口看不到:
我猜測原因可能是我在配置類中指定的接口層位置影響了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界面中出現了另一個接口的api:
但是這樣的效果并不好。大家試想一下,我們為什么要對接口分層呢?不就是為了將業務隔離么,這樣在一個界面中出現兩個接口層的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:標準接口
接口層2:其他接口
現在我們只要通過切換分組,就可以找到我們關注的接口層的api了。
下面貼出完整的配置類:
@Configuration @EnableSwagger2 public class SwaggerConfig { /** * 創建API應用 * apiInfo() 增加API相關信息 * 通過select()函數返回一個ApiSelectorBuilder實例,用來控制哪些接口暴露給Swagger來展現, * 本例采用指定掃描的包路徑來定義指定要建立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(); } /** * 創建該API的基本信息(這些基本信息會展現在文檔頁面中) * 訪問地址:http://ip:port/swagger-ui.html * * @return */ private ApiInfo apiInfo(String title, String version) { return new ApiInfoBuilder() .title(title) .description("更多請關注: http://www.ythuaji.com.cn") .termsOfServiceUrl("http://www.ythuaji.com.cn") .contact(new Contact("xqnode", "http://www.ythuaji.com.cn", "[email protected]")) .version(version) .build(); } }
至此,springboot集成swagger2完成,同時加了一個餐,還滿意吧?哈哈
以上這篇SpringBoot集成swagger-ui以及swagger分組顯示操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xqnode/article/details/86557784