ToB企服应用市场:ToB评测及商务社交产业平台

标题: 简单易懂,高效实用的接口文档编写技巧 [打印本页]

作者: 来自云龙湖轮廓分明的月亮    时间: 2023-6-27 20:58
标题: 简单易懂,高效实用的接口文档编写技巧
大家好!我是sum墨,一个一线的底层码农,平时喜欢研究和思考一些技术相关的问题并整理成文,限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
以下是正文!
接口文档是什么

接口文档是一个软件系统的重要组成部分,它描述了系统中所有可供外部应用程序使用的接口。简单来说,接口文档就是用来帮助开发者开发和对接系统的指南。
在软件开发过程中,不同的系统之间需要进行数据交互和信息传递,这就要求系统必须提供一些公开的接口。
接口文档的展现形式也很多如:Swagger、Word、PDF、Postman、开放平台文档等。不同的展现形式提供了不同的载体来呈现接口文档,但是最终的关键还是内容本身。
一份清晰、详细、准确的接口文档是开发团队是否能够准确理解和使用系统接口的关键。
总之,一份好的接口文档应该覆盖所有的接口使用场景,详尽而不冗长,方便开发者快速查找和使用。而对于不同的展现形式,开发者需要根据项目的需求和开发流程选择最适合的展现方式,从而提高开发效率和工作质量。
Swagger接口文档

Swagger是一套用于设计、构建、记录和使用RESTful API的工具集,可以自动生成API文档,并提供交互式UI,能够大大提高开发效率和协作效率。它支持多种编程语言和框架,能够生成多种展现形式,如Swagger UI、Swagger Editor和Swagger Codegen等工具。同时,Swagger还提供强大的API管理功能,包括API监控、调试、测试和安全性等,能够帮助开发者更好地管理和维护API。
展示一下

访问方式一

访问地址:http://localhost:8080/swagger-ui.html#/
首页


详情页


访问方式二

访问地址:http://localhost:8080/doc.html
首页


侧边栏


详情页



SpringBoot整合Swagger2

1、配置文件

SpringBoot项目pom.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <groupId>org.springframework.boot</groupId>
  7.         <artifactId>spring-boot-starter-parent</artifactId>
  8.         <version>2.2.6.RELEASE</version>
  9.         <relativePath/>
  10.     </parent>
  11.     <groupId>com.example</groupId>
  12.     <artifactId>springboot-swagger</artifactId>
  13.     <version>0.0.1-SNAPSHOT</version>
  14.     <name>springboot-swagger</name>
  15.     <description>Demo project for Spring Boot</description>
  16.     <properties>
  17.         <java.version>1.8</java.version>
  18.     </properties>
  19.     <dependencies>
  20.         <dependency>
  21.             <groupId>org.springframework.boot</groupId>
  22.             <artifactId>spring-boot-starter-web</artifactId>
  23.         </dependency>
  24.         <dependency>
  25.             <groupId>org.springframework.boot</groupId>
  26.             <artifactId>spring-boot-starter-test</artifactId>
  27.             <scope>test</scope>
  28.         </dependency>
  29.         
  30.         <dependency>
  31.             <groupId>io.springfox</groupId>
  32.             <artifactId>springfox-swagger2</artifactId>
  33.             <version>2.8.0</version>
  34.         </dependency>
  35.         <dependency>
  36.             <groupId>io.springfox</groupId>
  37.             <artifactId>springfox-swagger-ui</artifactId>
  38.             <version>2.7.0</version>
  39.         </dependency>
  40.         <dependency>
  41.             <groupId>com.github.xiaoymin</groupId>
  42.             <artifactId>swagger-bootstrap-ui</artifactId>
  43.             <version>1.9.2</version>
  44.         </dependency>
  45.     </dependencies>
  46.     <build>
  47.         <plugins>
  48.             <plugin>
  49.                 <groupId>org.springframework.boot</groupId>
  50.                 <artifactId>spring-boot-maven-plugin</artifactId>
  51.             </plugin>
  52.         </plugins>
  53.     </build>
  54. </project>
复制代码
这里需要注意一个版本对应的问题,如果使用了高版本的SpringBoot框架,低版本的Swagger,
会出现如下报错:
  1. org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
  2.         at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
  3.         at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
  4.         at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
  5.         at java.lang.Iterable.forEach(Iterable.java:75)
  6.         at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
  7.         at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
  8.         at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
  9.         at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
  10.         at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
  11.         at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
  12.         at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
  13.         at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
  14.         at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
  15.         at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
复制代码
这是因为:因为Springfox 使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher。
以下几个版本是兼容的
SpringBoot版本Swagger版本2.5.62.9.2SpringBoot版本Swagger版本2.6.53.0.02、项目代码

项目结构


SwaggerConfig.java
  1. package com.example.springbootswagger.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. @Configuration
  12. public class SwaggerConfig {
  13.     @Bean
  14.     public Docket createDocket() {
  15.         return new Docket(DocumentationType.SWAGGER_2)
  16.                 .apiInfo(apiInfo())
  17.                 .enable(true)
  18.                 .groupName("我的接口文档")
  19.                 .select()
  20.                 .apis(RequestHandlerSelectors.basePackage("com.example.springbootswagger.controller"))
  21.                 .paths(PathSelectors.any())
  22.                 .build();
  23.     }
  24.     private ApiInfo apiInfo() {
  25.         return new ApiInfoBuilder()
  26.                 //标题
  27.                 .title("凤求凰")
  28.                 //作者、链接、邮箱等
  29.                 .contact(new Contact(
  30.                         "司马相如",
  31.                         "https://hanyu.baidu.com/shici/detail?pid=ecb82707a98c418995c5a0c50b770af0&from=kg0",
  32.                         ""
  33.                 ))
  34.                 //描述
  35.                 .description("有一美人兮,见之不忘。\n" +
  36.                         "一日不见兮,思之如狂。\n" +
  37.                         "凤飞翱翔兮,四海求凰。\n" +
  38.                         "无奈佳人兮,不在东墙。\n" +
  39.                         "将琴代语兮,聊写衷肠。\n" +
  40.                         "何日见许兮,慰我彷徨。\n" +
  41.                         "愿言配德兮,携手相将。\n" +
  42.                         "不得於飞兮,使我沦亡。\n" +
  43.                         "凤兮凤兮归故乡,遨游四海求其凰。\n" +
  44.                         "时未遇兮无所将,何悟今兮升斯堂!\n" +
  45.                         "有艳淑女在闺房,室迩人遐毒我肠。\n" +
  46.                         "何缘交颈为鸳鸯,胡颉颃兮共翱翔!\n" +
  47.                         "凰兮凰兮从我栖,得托孳尾永为妃。\n" +
  48.                         "交情通意心和谐,中夜相从知者谁?\n" +
  49.                         "双翼俱起翻高飞,无感我思使余悲。")
  50.                 //更新说明
  51.                 .termsOfServiceUrl("这是第一版")
  52.                 //版本号
  53.                 .version("1.0.0").build();
  54.     }
  55. }
复制代码
TestController.java
  1. package com.example.springbootswagger.controller;
  2. import com.example.springbootswagger.req.AddReq;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiImplicitParam;
  5. import io.swagger.annotations.ApiImplicitParams;
  6. import io.swagger.annotations.ApiOperation;
  7. import org.springframework.web.bind.annotation.*;
  8. @RestController
  9. @Api(tags = {"测试接口类"}, hidden = true)
  10. @RequestMapping("/test")
  11. public class TestController {
  12.     @ApiOperation("GET请求,查询方法")
  13.     @GetMapping("/query")
  14.     public String query() {
  15.         return "查询成功";
  16.     }
  17.     @ApiImplicitParams({
  18.             @ApiImplicitParam(name = "param1", value = "参数1", required = true),
  19.             @ApiImplicitParam(name = "param2", value = "参数2", required = false)
  20.     })
  21.     @ApiOperation("PUT请求,添加方法")
  22.     @PutMapping("/update")
  23.     public String update(
  24.             @RequestParam(required = true) String param1,
  25.             @RequestParam(required = false) String param2) {
  26.         return "更新成功";
  27.     }
  28.     @ApiOperation("POST请求,修改方法")
  29.     @PostMapping("/add")
  30.     public String add(@RequestBody AddReq addReq) {
  31.         return "添加成功";
  32.     }
  33.     @ApiImplicitParam(name = "id", value = "用户ID", required = true)
  34.     @ApiOperation("DELETE请求,删除方法")
  35.     @DeleteMapping("/del")
  36.     public String del(Long id) {
  37.         return "删除成功";
  38.     }
  39. }
复制代码
AddReq.java
  1. package com.example.springbootswagger.req;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. @ApiModel("添加参数")
  5. public class AddReq {
  6.     @ApiModelProperty("名字")
  7.     private String name;
  8.     @ApiModelProperty("密码")
  9.     private String password;
  10.     public String getName() {
  11.         return name;
  12.     }
  13.     public void setName(String name) {
  14.         this.name = name;
  15.     }
  16.     public String getPassword() {
  17.         return password;
  18.     }
  19.     public void setPassword(String password) {
  20.         this.password = password;
  21.     }
  22. }
复制代码
SpringbootSwaggerApplication.java
  1. package com.example.springbootswagger;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  5. @EnableSwagger2
  6. @SpringBootApplication
  7. public class SpringbootSwaggerApplication {
  8.     public static void main(String[] args) {
  9.         SpringApplication.run(SpringbootSwaggerApplication.class, args);
  10.     }
  11. }
复制代码
Word、PDF等文件类接口文档

为什么还需要文件类接口文档呢?首先,Swagger文档存在兼容性问题,有些系统不支持,这时候文件类接口文档就能派上用场。其次,Swagger文档不能离线使用,如果网络不稳定或者没有网络连接,就无法查看接口文档,而文件类接口文档可以在任何时候离线浏览。
之前我有写过一篇关于系统间交互的文章:多方合作时,系统间的交互是怎么做的?,系统间的接口交互一般提供的都是文件类文档,像Word、PDF这种。
在实际开发中,我发现不同的开发团队提供的接口文档格式存在很大的差异。有些团队提供的文档非常详细,甚至会给出调用示例代码,而有些团队则只提供了非常简单的接口说明,甚至连参数说明都没有。这种情况下,如果接口文档很详细,我可以很快地进行调试和联调,但是如果文档不够详细,很容易遇到各种坑(一言难尽的坑),必须要去找对方的开发。
我觉得一份好的接口文档应该包括以下内容:
以下是我认为样式较为简洁和清晰的 Word 模板接口文档示例(最下方是模板下载链接):

模板下载链接

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4