1. 手写文档存在的问题

  • 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时。
  • 接口返回结果不明确
  • 不能直接在线测试接口,通常需要使用工具,比如:Postman
  • 接口文档太多,不好管理

2. 使用 Swagger 解决问题

Swagger 也就是为了解决这个问题,当然也不能说 Swagger 就一定是完美的,当然也有缺点,最明显的就是代码植入性比较强。

3. Maven

增加 Swagger2 所需依赖,pom.xml 配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Swagger2 Begin -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<!-- Swagger2 End -->

4. 配置 Swagger2

注意:RequestHandlerSelectors.basePackage("cn.panyucable.springboot.controller")Controller 包路径,不然生成的文档扫描不到接口

创建一个名为 Swagger2Config 的 Java 配置类,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.panyucable.springboot.controller"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("iToken API 文档")
.description("iToken API 网关接口,http://www.zysheep.github.io")
.termsOfServiceUrl("http://www.zysheep.github.io")
.version("1.0.0")
.build();
}
}

4.1 启用 Swagger2

1
2
3
4
5
6
7
//Application` 中加上注解 `@EnableSwagger2` 表示开启 `Swagger
@EnableSwagger2
public class ServiceAdminApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAdminApplication.class, args);
}
}

4.2 使用 Swagger2

Controller中增加Swagger2 相关注解,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@ApiOperation(value = "管理员分页查询")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "path"),
@ApiImplicitParam(name = "pageSize", value = "笔数", required = true, dataType = "int", paramType = "path"),
@ApiImplicitParam(name = "tbSysUserJson", value = "管理员对象 JSON 字符串", required = false, dataTypeClass = String.class, paramType = "json")
})
@RequestMapping(value = "page/{pageNum}/{pageSize}", method = RequestMethod.GET)
public String page(
@PathVariable(required = true) int pageNum,
@PathVariable(required = true) int pageSize,
@RequestParam(required = false) String tbSysUserJson
) throws Exception {

return "Hello Swagger2";
}

4.3 Swagger 注解说明

Swagger 通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

  • @Api:修饰整个类,描述 Controller 的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP 响应其中 1 个描述
  • @ApiResponses:HTTP 响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError:发生错误返回的信息
  • @ApiImplicitParam:一个请求参数
  • @ApiImplicitParams:多个请求参数

4.4访问 Swagger2

访问地址:http://ip:port/swagger-ui.html