简介
API 一定要开发文档配合,移动端只需要根据开发文档进行开发。
传统开发文档的问题:格式随意,更新不及时
Swagger 的目标是为 REST APIS 定义一个标准的,与语言无关的接口。使人和计算机在看不到源码或者看不到文档情况下理解各种服务的功能。
简单说, swagger 能够根据代码中的注解,自动生成 api 文档,并提供测试接口。
swagger 和 swagger2 是两个不同版本,swagger2 也叫 springfox,就是 swagger-springmvc 的升级版。
使用:
添加依赖
| 12
 3
 4
 5
 
 | <dependency><groupId>io.springfox</groupId>
 <artifactId>springfox-swagger2</artifactId>
 <version>2.9.2</version>
 </dependency>
 
 | 
在 api 中插入配置类
SwaggerConfig.java
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 
 | @Configuration@EnableSwagger2
 public class SwaggerConfig {
 
 
 
 
 
 @Bean
 public Docket api() {
 return new Docket(DocumentationType.SWAGGER_2).select().build();
 }
 }
 
 | 
访问 域名:端口号/swagger-ui.html 访问开发文档
swagger2 的常用注解
| 12
 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
 
 | 
 
 
 
 
 
 
 @ApiImplictParam({
 @ApiImplictParam(value = "昵称", name = "nickName", dataType = "String", required = true)
 @ApiImplictParam(value = "邮箱", name = "email", dataType = "String", required = true)
 })
 
 
 
 
 @ApiModel(description = "游记")
 
 
 @APIModelProperty(value = "标题", name = "title", dataType = "String", required = true)
 
 
 
 @ApiResponse({
 @ApiResponse(code = 200, message = "用户注册成功"),
 @ApiResponse(code = 401, message = "没有登录"),
 })
 
 
 @ApiIgnore
 public class UserController
 
 | 
Models 可以看到所有的模型对象。