本文介紹了Spring Boot 項目中使用Swagger2的示例,分享給大家,具體如下:
添加Swagger2依賴
在pom.xml中加入Swagger2的依賴
1
2
3
4
5
6
7
8
9
10
|
< dependency > < groupId >io.springfox</ groupId > < artifactId >springfox-swagger2</ artifactId > < version >2.2.2</ version > </ dependency > < dependency > < groupId >io.springfox</ groupId > < artifactId >springfox-swagger-ui</ artifactId > < version >2.2.2</ version > </ dependency > |
創建Swagger2配置類
在Application.java同級創建Swagger2的配置類Swagger2。
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
|
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; @Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage( "你自己的外部接口包名稱" )) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title( "詞網Neo4j RESTful APIs" ) .description( "The Neo4j RESTful APIs description/" ) .termsOfServiceUrl( "" ) .contact( "李慶海" ) .version( "5.0" ) .build(); } } |
添加文檔內容
在完成了上述配置后,其實已經可以生產文檔內容,但是這樣的文檔主要針對請求本身,而描述主要來源于函數等命名產生,對用戶并不友好,我們通常需要自己增加一些說明來豐富文檔內容。
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
|
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; /** * 系統用戶Controller * * @author 李慶海 * */ @Api (value = "系統用戶接口" , tags = "系統管理" ) @RestController @RequestMapping ( "/v3/edu/users" ) public class UserController { @Autowired private UserService userService; /** * 添加用戶,注冊 * * @param loginName * 登錄賬號 * @param userName * 用戶名稱 * @param password * 登錄密碼 * @param roleId * 用戶角色 * @return * @throws ResourceExistsException */ @ApiOperation (value = "添加用戶" ) @PostMapping ( "/" ) public JsonResult create( @ApiParam (name = "loginName" , value = "登錄賬號" , required = true ) @RequestParam (required = true ) @RequestBody String loginName, @ApiParam (name = "userName" , value = "用戶名稱" , required = true ) @RequestParam (required = true ) @RequestBody String userName, @ApiParam (name = "password" , value = "登錄密碼" , required = true ) @RequestParam (required = true ) @RequestBody String password, @ApiParam (name = "roleId" , value = "用戶角色編號" , required = true ) @RequestParam (required = true ) @RequestBody String roleId) throws ResourceExistsException { boolean exists = this .userService.exists(loginName); if (exists) { throw new ResourceExistsException(loginName); } User user = userService.create(loginName, password, userName, roleId); return new JsonResult(user); } } |
查看API
啟動Spring Boot程序,訪問:http://localhost:8080/swagger-ui.html
API文檔訪問與調試
Swagger除了查看接口功能外,還提供了調試測試功能,我們可以點擊上圖中右側的Model Schema(黃色區域:它指明了數據結構),此時Value中就有了user對象的模板,我們只需要稍適修改,點擊下方Try it out!按鈕,即可完成了一次請求調用!可以通過幾個GET請求來驗證之前的POST請求是否正確。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/a133abf8e836