【接口设计】用 Swagger 实现接口文档
用 Swagger 实现接口文档
- 1.配置 Swagger
- 1.1 添加 Swagger 依赖
- 1.2 创建 Swagger 配置类
- 2.编写接口文档
在项目开发中,一般都是由前后端工程师共同定义接口,编写接口文档,之后大家根据这个接口文档进行开发、维护。为了便于编写和维护稳定,可以使用 Swagger 来编写 API 接口文档,以提升团队的沟通效率。
下面演示如何在 Spring Boot 中继承 Swagger。
1.配置 Swagger
1.1 添加 Swagger 依赖
<!--Swagger依赖-->
<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>
1.2 创建 Swagger 配置类
创建 Swagger 配置类,完成相关配置项,见以下代码:
package com.example.demo.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
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;/*** Swagger 配置类* 在与 Spring Boot 集成时,放在与 Application.java 同级的目录下*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {/*** 创建 API 应用* 本例采用指定扫描的包路径来定义指定要建立 API 的目录*/@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).paths(PathSelectors.any()).build();}/*** 创建该 API 的基本信息(这些基本信息会展现在文档页面中)* 访问地址:http://项目实际地址/swagger-ui.html*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title(" RESTful APIs").description("RESTful APIs").termsOfServiceUrl("http://localhost:8080/").contact("pipi").version("1.0").build();}
}
@Configuration
:让 Spring 来加载该类配置。@EnableSwagger2
:启用Swagger2.createRestApi
函数创建 Docket 的 Bean。apiInfo()
:用来展示该 API 的基本信息。select()
:返回一个 ApiSelectorBuilder 实例,用来控制哪些接口暴露给 Swagger 来展现。apis(RequestHandlerSelectors.basePackage())
:配置包扫描路径。Swagger 会扫描包下所有 Controler 定义的 API,并产生文档内容。如果不想产生 API,则使用注解@ApiIgnore
。
2.编写接口文档
在完成上述配置后,即生成了文档,但是这样生成的文档主要针对请求本身,而描述自动根据方法等命名产生,对用户并不友好。所以,通常需要自己增加一些说明以丰富文档内容。可以通过以下注解来增加说明。
@Api
:描述类/接口的主要用途。@ApiOperation
:描述方法用途,给 API 增加说明。@ApiImplicitParam
:描述方法的参数,给参数增加说明。@ApiImplicitParams
:描述方法的参数(Multi-Params),给参数增加说明。@ApiIgnore
:忽略某类/方法/参数的文档。
具体使用方法见以下代码:
package com.example.demo.controller;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;@RestController
public class HelloWorldController {@ApiOperation(value = "hello", notes = "notes")@RequestMapping("/hello")public String hello() throws Exception {return "HelloWorld ,Spring Boot!";}// 使用该注解忽略这个 API@ApiIgnore@RequestMapping(value = "/ignoreApi")public String ignoreApi() {return "HelloWorld ,Spring Boot!";}
}
完成上述代码后,启动项目,访问 http://localhost:8080/swagger-ui.html
就能看到所展示的 RESTful API 的页面,可以通过单击具体的 API 测试请求,来查看代码中配置的信息,以及参数的描述信息。