swagger 是一款restful接口的文檔在線自動生成+功能測試功能軟件。本文簡單介紹了在項目中集成swagger的方法和一些常見問題。如果想深入分析項目源碼,了解更多內容,見參考資料。
swagger 是一個規(guī)范和完整的框架,用于生成、描述、調用和可視化 restful 風格的 web 服務。總體目標是使客戶端和文件系統(tǒng)作為服務器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務器端的代碼,允許api來始終保持同步。swagger 讓部署管理和使用功能強大的api從未如此簡單。
對于搬磚的同學來說,寫接口容易,寫接口文檔很煩,接口變動,維護接口文檔就更更更煩,所以經(jīng)常能發(fā)現(xiàn)文檔與程序不匹配。
等過一段時間就連開發(fā)者也蒙圈了
swagger2快速方便的解決了以上問題。一個能與spring mvc程序配合組織出強大restful api文檔的新寵兒。
下面直接上代碼
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?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)建配置類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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)計結果輸出接口" ) .version( "1.0" ) .build(); } } |
controller編寫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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調用的接口,沒有任何的業(yè)務邏輯" ) @requestmapping (value = { "/test" },method = requestmethod.get) public string testhello(){ return "hello" ; } } |
代碼完成,準備看效果
點擊try it out!
是不是很詳細,很高大上。
注:集成過程中剛開始用的swagger2.2.2版本,會在首頁出現(xiàn)一個error的錯誤提醒
1
|
{“schemavalidationmessages”:[{“level”:”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 又能獲取 結果
1
|
{“swagger”:” 2.0 ”,”info”:{“version”:” 1.0 ”,”title”:”廊坊委貸大數(shù)據(jù)統(tǒng)計結果輸出接口”,”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調用的接口,沒有任何的業(yè)務邏輯”,”operationid”:”testhellousingget”,”consumes”:[“application/json”],”produces”:[“/“],”responses”:{“ 200 ”:{“description”:”ok”,”schema”:{“type”:”string”}},” 401 ”:{“description”:”unauthorized”},” 403 ”:{“description”:”forbidden”},” 404 ”:{“description”:”not found”}}}}}} |
具體原因本人不明,換成2.7.0版本以后沒在出現(xiàn)。
總結
以上所述是小編給大家介紹的springboot集成swagger的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/Smilence1024/p/8004814.html?utm_source=tuicool&utm_medium=referral