導語
相信大家無論是做前端還是做后端的,都被接口接口文檔所折磨過,前端抱怨接口文檔和后端給的不一致,后端抱怨寫接口文檔很麻煩,所以swagger就誕生了。直接配置即可自動生成接口文檔,而且提供了高效的api測試
話不多說直接開干
導入springboot集成swagger所需要的依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!--web方便測試--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!-- swagger2核心包 --> <dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger2</artifactid> <version> 2.9 . 2 </version> </dependency> <!-- swagger-ui 可視化界面 --> <dependency> <groupid>io.springfox</groupid> <artifactid>springfox-swagger-ui</artifactid> <version> 2.9 . 2 </version> </dependency> |
swagger可視化界面可分為三個區域
swagger相關配置
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
|
package com.example.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; 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; import java.util.arraylist; @configuration @enableswagger2 //開啟swagger的使用 public class swaggerconfig { @bean //swagger的使用主要是要將docket對象傳入ioc容器 public docket docket(){ return new docket(documentationtype.swagger_2) .apiinfo(apiinfo()) //關于文檔的各種信息 .enable( true ) //使swagger生效 .groupname( "常安祖" ) .select() //選擇掃描的接口 .apis(requesthandlerselectors.basepackage( "com.example.controller" )) //指定掃描的接口 .build(); } public apiinfo apiinfo(){ contact contact = new contact( "長安" , "https://blog.csdn.net/weixin_45647685" , "[email protected]" );//個人的聯系方式 return new apiinfo( "長安的文檔" , "長安的開發文檔" , "1.0" , "urn:tos" , null , "apache 2.0" , "http://www.apache.org/licenses/license-2.0" , new arraylist());//文檔的各種信息 } } |
@apimodel( ) //主要用來標注返回的實體類
@apimodelproperty( ) //主要用來標注實體類中的屬性
案例:
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
|
@apimodel ( "用戶的實體類" ) public class user implements serializable { @apimodelproperty ( "用戶的id" ) private integer id; @apimodelproperty ( "用戶的姓名" ) private string name; @apimodelproperty ( "用戶的年紀" ) private integer age; public integer getid() { return id; } public user(integer id, string name, integer age) { this .id = id; this .name = name; this .age = age; } public void setid(integer id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } public integer getage() { return age; } public void setage(integer age) { this .age = age; } } |
@apimodelproperty用來標注api接口
案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.yangzihao.controller; import com.yangzihao.entity.user; import io.swagger.annotations.apimodelproperty; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestmapping; @restcontroller public class usercontroller { @apimodelproperty ( "得到一個user" ) @getmapping ( "/getuser" ) public user getuser(){ return new user( 1 , "測試" , 18 ); } } |
進入swagger可視化界面
使用swagger進行接口測試
執行
到此這篇關于手把手教你springboot快速集成swagger的配置過程的文章就介紹到這了,更多相關springboot集成swagger內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_45647685/article/details/113850616