前言
swagger,中文“拽”的意思。它是一個功能強大的api框架,它的集成非常簡單,不僅提供了在線文檔的查閱,
而且還提供了在線文檔的測試。另外swagger很容易構建restful風格的api。
一、swagger概述
swagger是一組圍繞openapi規范構建的開源工具,可幫助設計、構建、記錄和使用rest api。
簡單說下,它的出現就是為了方便進行測試后臺的restful形式的接口,實現動態的更新,當我們在后臺的接口
修改了后,swagger可以實現自動的更新,而不需要認為的維護這個接口進行測試。
二、swagger常用注解
swagger通過注解表明該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。
- @api:修飾整個類,描述controller的作用
- @apioperation:描述一個類的一個方法,或者說一個接口
- @apiparam:單個參數描述
- @apimodel:用對象來接收參數
- @apiproperty:用對象接收參數時,描述對象的一個字段
- @apiresponse:http響應其中1個描述
- @apiresponses:http響應整體描述
- @apiignore:使用該注解忽略這個api
- @apierror :發生錯誤返回的信息
- @apiparamimplicitl:一個請求參數
- @apiparamsimplicit 多個請求參數
三、springboot整合swagger
3.1 添加依賴
1
2
3
4
5
6
7
8
9
10
|
<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> |
3.2 添加swaggerconfiguration
通過@configuration注解,表明它是一個配置類,@enableswagger2開啟swagger2。
apiinfo()配置一些基本的信息。apis()指定掃描的包會生成文檔。
再通過createrestapi函數創建docket的bean之后,apiinfo()用來創建該api的基本信息(這些基本信息會
展現在文檔頁面中)。select()函數返回一個apiselectorbuilder實例用來控制哪些接口暴露給swagger來
展現,本例采用指定掃描的包路徑來定義,swagger會掃描該包下所有controller定義的api,并產生文檔內容
(除了被@apiignore指定的請求)。
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
44
45
46
47
48
49
50
51
52
53
|
package com.lance.learn.springbootswagger.configuration; 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; /** * @author lance(zyh) * @function swagger啟動配置類 * @date 2018-07-09 21:24 */ @configuration @enableswagger2 public class swaggerconfiguration { /** * swagger2的配置文件,這里可以配置swagger2的一些基本的內容,比如掃描的包等等 * @return */ @bean public docket createrestfulapi(){ return new docket(documentationtype.swagger_2) .pathmapping( "/" ) .select() .apis(requesthandlerselectors.basepackage( "com.lance.learn.springbootswagger.controller" )) //暴露接口地址的包路徑 .paths(pathselectors.any()) .build(); } /** * 構建 api文檔的詳細信息函數,注意這里的注解引用的是哪個 * @return */ private apiinfo apiinfo(){ return new apiinfobuilder() //頁面標題 .title( "spring boot 測試使用 swagger2 構建restful api" ) //創建人 //版本號 .version( "1.0" ) //描述 .description( "api 描述" ) .build(); } } |
3.3 controller文檔內容
描述主要來源于函數等命名產生,對用戶并不友好,我們通常需要自己增加一些說明來豐富文檔內容。
如下所示,我們通過@apioperation注解來給api增加說明、通過@apiimplicitparams、@apiimplicitparam
注解來給參數增加說明。
1)實例一
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package com.lance.learn.springbootswagger.controller; import com.lance.learn.springbootswagger.bean.book; import io.swagger.annotations.apiimplicitparam; import io.swagger.annotations.apiimplicitparams; import io.swagger.annotations.apioperation; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.apiignore; import java.util.*; /** * @author lance(zyh) * @function * @date 2018-07-09 21:39 */ @restcontroller @requestmapping (value = "/bookcurd" ) public class bookcontroller { map< long , book> books = collections.synchronizedmap( new hashmap< long , book>()); @apioperation (value= "獲取圖書列表" , notes= "獲取圖書列表" ) @requestmapping (value={ "" }, method= requestmethod.get) public list<book> getbook() { list<book> book = new arraylist<>(books.values()); return book; } @apioperation (value= "創建圖書" , notes= "創建圖書" ) @apiimplicitparam (name = "book" , value = "圖書詳細實體" , required = true , datatype = "book" ) @requestmapping (value= "" , method=requestmethod.post) public string postbook( @requestbody book book) { books.put(book.getid(), book); return "success" ; } @apioperation (value= "獲圖書細信息" , notes= "根據url的id來獲取詳細信息" ) @apiimplicitparam (name = "id" , value = "id" , required = true , datatype = "long" ,paramtype = "path" ) @requestmapping (value= "/{id}" , method=requestmethod.get) public book getbook( @pathvariable long id) { return books.get(id); } @apioperation (value= "更新信息" , notes= "根據url的id來指定更新圖書信息" ) @apiimplicitparams ({ @apiimplicitparam (name = "id" , value = "圖書id" , required = true , datatype = "long" ,paramtype = "path" ), @apiimplicitparam (name = "book" , value = "圖書實體book" , required = true , datatype = "book" ) }) @requestmapping (value= "/{id}" , method= requestmethod.put) public string putuser( @pathvariable long id, @requestbody book book) { book book1 = books.get(id); book1.setname(book.getname()); book1.setprice(book.getprice()); books.put(id, book1); return "success" ; } @apioperation (value= "刪除圖書" , notes= "根據url的id來指定刪除圖書" ) @apiimplicitparam (name = "id" , value = "圖書id" , required = true , datatype = "long" ,paramtype = "path" ) @requestmapping (value= "/{id}" , method=requestmethod.delete) public string deleteuser( @pathvariable long id) { books.remove(id); return "success" ; } @apiignore //使用該注解忽略這個api @requestmapping (value = "/hi" , method = requestmethod.get) public string jsontest() { return " hi you!" ; } } |
2)實例二
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.lance.learn.springbootswagger.controller; import com.lance.learn.springbootswagger.bean.user; import io.swagger.annotations.apiimplicitparam; import io.swagger.annotations.apiimplicitparams; import io.swagger.annotations.apioperation; import org.springframework.web.bind.annotation.*; import java.util.*; /** * @author lance(zyh) * @function * @date 2018-07-09 22:00 */ @restcontroller @requestmapping (value= "/users" ) public class userdetailcontroller { static map< long , user> users = collections.synchronizedmap( new hashmap< long , user>()); @apioperation (value= "獲取用戶列表" , notes= "" ) @requestmapping (value={ "" }, method= requestmethod.get) public list<user> getuserlist() { list<user> r = new arraylist<user>(users.values()); return r; } @apioperation (value= "創建用戶" , notes= "根據user對象創建用戶" ) @apiimplicitparam (name = "user" , value = "用戶詳細實體user" , required = true , datatype = "user" ) @requestmapping (value= "" , method=requestmethod.post) public string postuser( @requestbody user user) { users.put(user.getid(), user); return "success" ; } @apioperation (value= "獲取用戶詳細信息" , notes= "根據url的id來獲取用戶詳細信息" ) @apiimplicitparam (name = "id" , value = "用戶id" , required = true , datatype = "long" ) @requestmapping (value= "/{id}" , method=requestmethod.get) public user getuser( @pathvariable long id) { return users.get(id); } @apioperation (value= "更新用戶詳細信息" , notes= "根據url的id來指定更新對象,并根據傳過來的user信息來更新用戶詳細信息" ) @apiimplicitparams ({ @apiimplicitparam (name = "id" , value = "用戶id" , required = true , datatype = "long" ), @apiimplicitparam (name = "user" , value = "用戶詳細實體user" , required = true , datatype = "user" ) }) @requestmapping (value= "/{id}" , method=requestmethod.put) public string putuser( @pathvariable long id, @requestbody user user) { user u = new user(); users.put(id, u); return "success" ; } @apioperation (value= "刪除用戶" , notes= "根據url的id來指定刪除對象" ) @apiimplicitparam (name = "id" , value = "用戶id" , required = true , datatype = "long" ) @requestmapping (value= "/{id}" , method=requestmethod.delete) public string deleteuser( @pathvariable long id) { users.remove(id); return "success" ; } } |
3.4 web界面查看
四、項目代碼地址
https://github.com/lancetobigdata/springbootlearning/tree/develop/springboot-swagger
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/zhangyinhua/p/9286391.html