小編給大家分享一下SpringBoot如何集成swagger,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)連城免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
Swagger 是一款RESTFUL接口的文檔在線自動生成+功能測試功能軟件。本文簡單介紹了在項目中集成swagger的方法和一些常見問題。如果想深入分析項目源碼,了解更多內(nèi)容,見參考資料。
Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風格的 Web 服務??傮w目標是使客戶端和文件系統(tǒng)作為服務器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務器端的代碼,允許API來始終保持同步。Swagger 讓部署管理和使用功能強大的API從未如此簡單。
對于搬磚的同學來說,寫接口容易,寫接口文檔很煩,接口變動,維護接口文檔就更更更煩,所以經(jīng)常能發(fā)現(xiàn)文檔與程序不匹配。
等過一段時間就連開發(fā)者也蒙圈了
Swagger2快速方便的解決了以上問題。一個能與Spring MVC程序配合組織出強大RESTful API文檔的新寵兒。
下面直接上代碼
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhongxin.wealth</groupId> <artifactId>wealthweb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>wealthweb</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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> </dependencies> </project>
創(chuàng)建配置類
package com.zhongxin.wealth.apiConfig; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Created by DingYS on 2017/12/8. */ @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.zhongxin.wealth.web")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("廊坊委貸大數(shù)據(jù)統(tǒng)計結(jié)果輸出接口") .version("1.0") .build(); } }
controller編寫
package com.zhongxin.wealth.web; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by DingYS on 2017/12/7. */ @RestController @RequestMapping("/hello") public class HelloWordController { @ApiOperation(value="測試接口", notes="這只是一個測試controller調(diào)用的接口,沒有任何的業(yè)務邏輯") @RequestMapping(value = {"/test"},method = RequestMethod.GET) public String testHello(){ return "hello"; } }
代碼完成,準備看效果
點擊Try it out!
是不是很詳細,很高大上。
注:集成過程中剛開始用的swagger2.2.2版本,會在首頁出現(xiàn)一個error的錯誤提醒
{“schemaValidationMessages”:[{“l(fā)evel”:”error”,”message”:”Can't read from file http://127.0.0.1:8888/v2/api-docs"}]}
但是瀏覽器訪問:http://127.0.0.1:8888/v2/api-docs 又能獲取 結(jié)果
{“swagger”:”2.0”,”info”:{“version”:”1.0”,”title”:”廊坊委貸大數(shù)據(jù)統(tǒng)計結(jié)果輸出接口”,”contact”:{},”license”:{}},”host”:”127.0.0.1:8888”,”basePath”:”/“,”tags”:[{“name”:”hello-word-controller”,”description”:”Hello Word Controller”}],”paths”:{“/hello/test”:{“get”:{“tags”:[“hello-word-controller”],”summary”:”測試接口”,”description”:”這只是一個測試controller調(diào)用的接口,沒有任何的業(yè)務邏輯”,”operationId”:”testHelloUsingGET”,”consumes”:[“application/json”],”produces”:[“/“],”responses”:{“200”:{“description”:”O(jiān)K”,”schema”:{“type”:”string”}},”401”:{“description”:”Unauthorized”},”403”:{“description”:”Forbidden”},”404”:{“description”:”Not Found”}}}}}}
具體原因本人不明,換成2.7.0版本以后沒在出現(xiàn)。
看完了這篇文章,相信你對“SpringBoot如何集成swagger”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
名稱欄目:SpringBoot如何集成swagger
標題URL:http://aaarwkj.com/article8/gojpop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、手機網(wǎng)站建設(shè)、服務器托管、建站公司、外貿(mào)建站、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)