本文介绍了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