1. 程式人生 > >SwaggerUI自動生成API文件(SwaggerUI+SpringBoot)

SwaggerUI自動生成API文件(SwaggerUI+SpringBoot)



SwaggerUI+SpringBoot
測試訪問地址 http://localhost:8080/v1/swagger-ui.html#/ 下面是具體配置 Maven依賴
<!-- Swagger --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.2.2</version></dependency><dependency>
<groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.2.2</version></dependency><!-- END Swagger -->
properties
server.servlet-path=/v1
Config
package com.unioncast.db.config;importstatic com.google
.common.base.Predicates.or;
importstatic springfox.documentation.builders.PathSelectors.regex;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.ResponseEntity
;
import com.google.common.base.Predicate;import com.google.common.base.Predicates;import springfox.documentation.builders.PathSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;/** * SwaggerConfig */@ConfigurationpublicclassSwaggerConfig{@Value("${server.servlet-path}")privateString pathMapping;privateApiInfo initApiInfo(){ApiInfo apiInfo =newApiInfo("資料儲存專案 Platform API",// 大標題 initContextInfo(),// 簡單的描述"1.0.0",// 版本"服務條款","後臺開發團隊",// 作者"The Apache License, Version 2.0",// 連結顯示文字"http://www.baidu.com"// 網站連結);return apiInfo;}privateString initContextInfo(){StringBuffer sb =newStringBuffer(); sb.append("REST API 設計在細節上有很多自己獨特的需要注意的技巧,並且對開發人員在構架設計能力上比傳統 API 有著更高的要求。").append("<br/>").append("以下是本專案的API文件");return sb.toString();}@BeanpublicDocket restfulApi(){System.out.println("http://localhost:8080"+ pathMapping +"/swagger-ui.html");returnnewDocket(DocumentationType.SWAGGER_2).groupName("RestfulApi")// .genericModelSubstitutes(DeferredResult.class).genericModelSubstitutes(ResponseEntity.class).useDefaultResponseMessages(true).forCodeGeneration(false).pathMapping(pathMapping)// base,最終呼叫介面後會和paths拼接在一起.select().paths(doFilteringRules()).build().apiInfo(initApiInfo());// .select().paths(Predicates.not(PathSelectors.regex("/error.*"))).build().apiInfo(initApiInfo());}/** * 設定過濾規則 這裡的過濾規則支援正則匹配 //若有靜態方法在此之前載入就會報集合相關的錯誤. * * @return */privatePredicate<String> doFilteringRules(){// return Predicates.not(PathSelectors.regex("/error.*"));// return or(regex("/hello.*"), regex("/rest/adxSspFinanceManagement.*"));//successreturn or(regex("/hello.*"),