简体中文 繁體中文 English Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français Japanese

站内搜索

搜索

活动公告

通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,将及时处理!
10-23 09:31

实用Swagger JSON配置教程一步步教你打造专业API文档

SunJu_FaceMall

3万

主题

153

科技点

3万

积分

大区版主

碾压王

积分
32103
发表于 2025-10-1 01:10:02 | 显示全部楼层 |阅读模式 [标记阅至此楼]

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

在当今的软件开发世界中,API(应用程序编程接口)扮演着连接不同软件系统的关键角色。无论是构建微服务架构、移动应用后端,还是第三方集成,良好的API文档都是确保开发者能够轻松理解和使用你的服务的关键。Swagger(现在称为OpenAPI规范)已经成为行业标准,为RESTful API提供了一套强大而灵活的文档框架。

本教程将深入探讨Swagger JSON配置,帮助你从零开始创建专业、全面且易于维护的API文档。无论你是API设计的新手还是有经验的开发者,本指南都将提供实用的步骤和最佳实践,让你的API文档达到专业水平。

Swagger基础

什么是Swagger?

Swagger是一套围绕OpenAPI规范构建的开源工具,用于设计、构建、记录和使用RESTful Web服务。它允许你描述API的结构,以便机器可以读取它们的能力。通过Swagger,你可以:

• 生成交互式API文档
• 自动生成客户端SDK代码
• 进行API测试
• 确保API设计与实现保持一致

OpenAPI规范

OpenAPI规范(OAS)是Swagger的基础,它定义了一个标准的、与语言无关的接口到RESTful API。最新的版本是OpenAPI 3.0,它引入了许多改进和新特性,如:

• 更好的可重用性
• 增强的安全支持
• 改进的内容协商
• 更灵活的回调支持
• 更强的链接支持

环境准备

在开始配置Swagger JSON之前,你需要准备一些工具和环境:

1. 文本编辑器

选择一个支持JSON语法高亮和验证的文本编辑器,如:

• Visual Studio Code
• WebStorm
• Sublime Text
• Atom

2. Swagger编辑器

Swagger提供了在线编辑器,可以帮助你实时预览和验证你的Swagger JSON配置:

Swagger Editor

3. 本地开发环境(可选)

如果你想在本地运行Swagger UI,可以设置一个简单的HTTP服务器:
  1. # 使用Node.js的http-server
  2. npm install -g http-server
  3. http-server -o
  4. # 或者使用Python的SimpleHTTPServer
  5. python -m http.server 8000
复制代码

创建基本的Swagger JSON配置

让我们从创建一个基本的Swagger JSON配置文件开始。这个文件通常命名为swagger.json或openapi.json。

最小配置示例
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0"
  6.   },
  7.   "paths": {}
  8. }
复制代码

这个最小配置包含了三个必需的部分:

1. openapi- 指定使用的OpenAPI规范版本
2. info- 提供API的元数据
3. paths- 定义API的路径和操作

完整的基本配置

让我们扩展这个基本配置,添加更多有用的信息:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "description": "这是一个示例API,用于演示Swagger JSON配置",
  6.     "version": "1.0.0",
  7.     "contact": {
  8.       "name": "API支持",
  9.       "email": "support@example.com",
  10.       "url": "https://example.com/support"
  11.     },
  12.     "license": {
  13.       "name": "MIT",
  14.       "url": "https://opensource.org/licenses/MIT"
  15.     }
  16.   },
  17.   "servers": [
  18.     {
  19.       "url": "https://api.example.com/v1",
  20.       "description": "生产服务器"
  21.     },
  22.     {
  23.       "url": "https://dev-api.example.com/v1",
  24.       "description": "开发服务器"
  25.     }
  26.   ],
  27.   "paths": {}
  28. }
复制代码

在这个扩展版本中,我们添加了:

• description- API的详细描述
• contact- 联系信息
• license- 许可证信息
• servers- API服务器列表

深入Swagger JSON结构

现在,让我们深入了解Swagger JSON的各个部分,以及如何配置它们来创建专业的API文档。

Info对象

info对象提供关于API的元数据,它是Swagger JSON的必需部分。以下是一个完整的info对象示例:
  1. "info": {
  2.   "title": "用户管理API",
  3.   "description": "这是一个完整的用户管理系统API,提供用户注册、登录、资料管理等功能。\n\n## 功能特点\n- 用户注册和认证\n- 用户资料管理\n- 角色和权限管理\n- 密码重置\n\n## 使用指南\n要使用此API,开发者需要先注册获取API密钥。",
  4.   "termsOfService": "https://example.com/terms",
  5.   "contact": {
  6.     "name": "API支持团队",
  7.     "email": "api-support@example.com",
  8.     "url": "https://example.com/contact"
  9.   },
  10.   "license": {
  11.     "name": "Apache 2.0",
  12.     "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
  13.   },
  14.   "version": "1.0.1"
  15. }
复制代码

Servers对象

servers数组指定API的目标服务器,可以包含多个环境:
  1. "servers": [
  2.   {
  3.     "url": "https://api.example.com/{version}",
  4.     "description": "生产服务器",
  5.     "variables": {
  6.       "version": {
  7.         "default": "v1",
  8.         "enum": ["v1", "v2"],
  9.         "description": "API版本"
  10.       }
  11.     }
  12.   },
  13.   {
  14.     "url": "https://dev-api.example.com/v1",
  15.     "description": "开发服务器"
  16.   },
  17.   {
  18.     "url": "https://staging-api.example.com/v1",
  19.     "description": "预发布服务器"
  20.   }
  21. ]
复制代码

在这个例子中,我们使用服务器变量来支持API版本控制。

Paths对象

paths对象是Swagger JSON的核心,它定义了API的路径和操作。让我们看一个完整的用户管理API示例:
  1. "paths": {
  2.   "/users": {
  3.     "get": {
  4.       "summary": "获取用户列表",
  5.       "description": "返回系统中的所有用户列表,支持分页和筛选",
  6.       "operationId": "getUsers",
  7.       "tags": ["用户管理"],
  8.       "parameters": [
  9.         {
  10.           "name": "page",
  11.           "in": "query",
  12.           "description": "页码",
  13.           "required": false,
  14.           "schema": {
  15.             "type": "integer",
  16.             "default": 1
  17.           }
  18.         },
  19.         {
  20.           "name": "limit",
  21.           "in": "query",
  22.           "description": "每页记录数",
  23.           "required": false,
  24.           "schema": {
  25.             "type": "integer",
  26.             "default": 20,
  27.             "maximum": 100
  28.           }
  29.         },
  30.         {
  31.           "name": "role",
  32.           "in": "query",
  33.           "description": "按角色筛选",
  34.           "required": false,
  35.           "schema": {
  36.             "type": "string",
  37.             "enum": ["admin", "user", "guest"]
  38.           }
  39.         }
  40.       ],
  41.       "responses": {
  42.         "200": {
  43.           "description": "成功获取用户列表",
  44.           "content": {
  45.             "application/json": {
  46.               "schema": {
  47.                 "type": "object",
  48.                 "properties": {
  49.                   "users": {
  50.                     "type": "array",
  51.                     "items": {
  52.                       "$ref": "#/components/schemas/User"
  53.                     }
  54.                   },
  55.                   "pagination": {
  56.                     "$ref": "#/components/schemas/Pagination"
  57.                   }
  58.                 }
  59.               }
  60.             }
  61.           }
  62.         },
  63.         "401": {
  64.           "description": "未授权"
  65.         },
  66.         "403": {
  67.           "description": "禁止访问"
  68.         }
  69.       },
  70.       "security": [
  71.         {
  72.           "bearerAuth": []
  73.         }
  74.       ]
  75.     },
  76.     "post": {
  77.       "summary": "创建新用户",
  78.       "description": "在系统中创建一个新用户",
  79.       "operationId": "createUser",
  80.       "tags": ["用户管理"],
  81.       "requestBody": {
  82.         "required": true,
  83.         "content": {
  84.           "application/json": {
  85.             "schema": {
  86.               "$ref": "#/components/schemas/NewUser"
  87.             }
  88.           }
  89.         }
  90.       },
  91.       "responses": {
  92.         "201": {
  93.           "description": "用户创建成功",
  94.           "content": {
  95.             "application/json": {
  96.               "schema": {
  97.                 "$ref": "#/components/schemas/User"
  98.               }
  99.             }
  100.           }
  101.         },
  102.         "400": {
  103.           "description": "无效的输入数据"
  104.         },
  105.         "409": {
  106.           "description": "用户已存在"
  107.         }
  108.       },
  109.       "security": [
  110.         {
  111.           "bearerAuth": []
  112.         }
  113.       ]
  114.     }
  115.   },
  116.   "/users/{userId}": {
  117.     "get": {
  118.       "summary": "获取用户详情",
  119.       "description": "根据用户ID获取用户详细信息",
  120.       "operationId": "getUserById",
  121.       "tags": ["用户管理"],
  122.       "parameters": [
  123.         {
  124.           "name": "userId",
  125.           "in": "path",
  126.           "required": true,
  127.           "description": "用户ID",
  128.           "schema": {
  129.             "type": "string",
  130.             "format": "uuid"
  131.           }
  132.         }
  133.       ],
  134.       "responses": {
  135.         "200": {
  136.           "description": "成功获取用户详情",
  137.           "content": {
  138.             "application/json": {
  139.               "schema": {
  140.                 "$ref": "#/components/schemas/User"
  141.               }
  142.             }
  143.           }
  144.         },
  145.         "404": {
  146.           "description": "用户不存在"
  147.         }
  148.       },
  149.       "security": [
  150.         {
  151.           "bearerAuth": []
  152.         }
  153.       ]
  154.     },
  155.     "put": {
  156.       "summary": "更新用户信息",
  157.       "description": "更新指定用户的信息",
  158.       "operationId": "updateUser",
  159.       "tags": ["用户管理"],
  160.       "parameters": [
  161.         {
  162.           "name": "userId",
  163.           "in": "path",
  164.           "required": true,
  165.           "description": "用户ID",
  166.           "schema": {
  167.             "type": "string",
  168.             "format": "uuid"
  169.           }
  170.         }
  171.       ],
  172.       "requestBody": {
  173.         "required": true,
  174.         "content": {
  175.           "application/json": {
  176.             "schema": {
  177.               "$ref": "#/components/schemas/UpdateUser"
  178.             }
  179.           }
  180.         }
  181.       },
  182.       "responses": {
  183.         "200": {
  184.           "description": "用户更新成功",
  185.           "content": {
  186.             "application/json": {
  187.               "schema": {
  188.                 "$ref": "#/components/schemas/User"
  189.               }
  190.             }
  191.           }
  192.         },
  193.         "400": {
  194.           "description": "无效的输入数据"
  195.         },
  196.         "404": {
  197.           "description": "用户不存在"
  198.         }
  199.       },
  200.       "security": [
  201.         {
  202.           "bearerAuth": []
  203.         }
  204.       ]
  205.     },
  206.     "delete": {
  207.       "summary": "删除用户",
  208.       "description": "从系统中删除指定用户",
  209.       "operationId": "deleteUser",
  210.       "tags": ["用户管理"],
  211.       "parameters": [
  212.         {
  213.           "name": "userId",
  214.           "in": "path",
  215.           "required": true,
  216.           "description": "用户ID",
  217.           "schema": {
  218.             "type": "string",
  219.             "format": "uuid"
  220.           }
  221.         }
  222.       ],
  223.       "responses": {
  224.         "204": {
  225.           "description": "用户删除成功"
  226.         },
  227.         "404": {
  228.           "description": "用户不存在"
  229.         }
  230.       },
  231.       "security": [
  232.         {
  233.           "bearerAuth": []
  234.         }
  235.       ]
  236.     }
  237.   }
  238. }
复制代码

这个示例展示了如何定义完整的CRUD操作,包括:

• 路径参数(userId)
• 查询参数(page,limit,role)
• 请求体(用于创建和更新用户)
• 响应定义(包括成功和错误响应)
• 安全要求(使用Bearer认证)

定义数据模型

在Swagger JSON中,数据模型在components.schemas部分定义。这些模型可以被路径和响应引用,确保API文档的一致性和可维护性。

基本数据模型

让我们为用户管理API定义一些基本的数据模型:
  1. "components": {
  2.   "schemas": {
  3.     "User": {
  4.       "type": "object",
  5.       "required": ["id", "username", "email", "role"],
  6.       "properties": {
  7.         "id": {
  8.           "type": "string",
  9.           "format": "uuid",
  10.           "description": "用户唯一标识符",
  11.           "readOnly": true,
  12.           "example": "550e8400-e29b-41d4-a716-446655440000"
  13.         },
  14.         "username": {
  15.           "type": "string",
  16.           "description": "用户名",
  17.           "minLength": 3,
  18.           "maxLength": 30,
  19.           "pattern": "^[a-zA-Z0-9_-]+$",
  20.           "example": "johndoe"
  21.         },
  22.         "email": {
  23.           "type": "string",
  24.           "format": "email",
  25.           "description": "用户电子邮箱",
  26.           "example": "john.doe@example.com"
  27.         },
  28.         "firstName": {
  29.           "type": "string",
  30.           "description": "名字",
  31.           "example": "John"
  32.         },
  33.         "lastName": {
  34.           "type": "string",
  35.           "description": "姓氏",
  36.           "example": "Doe"
  37.         },
  38.         "role": {
  39.           "type": "string",
  40.           "description": "用户角色",
  41.           "enum": ["admin", "user", "guest"],
  42.           "example": "user"
  43.         },
  44.         "createdAt": {
  45.           "type": "string",
  46.           "format": "date-time",
  47.           "description": "创建时间",
  48.           "readOnly": true,
  49.           "example": "2023-01-01T00:00:00Z"
  50.         },
  51.         "updatedAt": {
  52.           "type": "string",
  53.           "format": "date-time",
  54.           "description": "更新时间",
  55.           "readOnly": true,
  56.           "example": "2023-01-01T00:00:00Z"
  57.         }
  58.       }
  59.     },
  60.     "NewUser": {
  61.       "type": "object",
  62.       "required": ["username", "email", "password"],
  63.       "properties": {
  64.         "username": {
  65.           "type": "string",
  66.           "description": "用户名",
  67.           "minLength": 3,
  68.           "maxLength": 30,
  69.           "pattern": "^[a-zA-Z0-9_-]+$",
  70.           "example": "johndoe"
  71.         },
  72.         "email": {
  73.           "type": "string",
  74.           "format": "email",
  75.           "description": "用户电子邮箱",
  76.           "example": "john.doe@example.com"
  77.         },
  78.         "password": {
  79.           "type": "string",
  80.           "format": "password",
  81.           "description": "密码",
  82.           "minLength": 8,
  83.           "writeOnly": true,
  84.           "example": "SecurePassword123!"
  85.         },
  86.         "firstName": {
  87.           "type": "string",
  88.           "description": "名字",
  89.           "example": "John"
  90.         },
  91.         "lastName": {
  92.           "type": "string",
  93.           "description": "姓氏",
  94.           "example": "Doe"
  95.         }
  96.       }
  97.     },
  98.     "UpdateUser": {
  99.       "type": "object",
  100.       "properties": {
  101.         "email": {
  102.           "type": "string",
  103.           "format": "email",
  104.           "description": "用户电子邮箱",
  105.           "example": "john.doe@example.com"
  106.         },
  107.         "firstName": {
  108.           "type": "string",
  109.           "description": "名字",
  110.           "example": "John"
  111.         },
  112.         "lastName": {
  113.           "type": "string",
  114.           "description": "姓氏",
  115.           "example": "Doe"
  116.         },
  117.         "role": {
  118.           "type": "string",
  119.           "description": "用户角色",
  120.           "enum": ["admin", "user", "guest"],
  121.           "example": "user"
  122.         }
  123.       }
  124.     },
  125.     "Pagination": {
  126.       "type": "object",
  127.       "properties": {
  128.         "page": {
  129.           "type": "integer",
  130.           "description": "当前页码",
  131.           "example": 1
  132.         },
  133.         "limit": {
  134.           "type": "integer",
  135.           "description": "每页记录数",
  136.           "example": 20
  137.         },
  138.         "total": {
  139.           "type": "integer",
  140.           "description": "总记录数",
  141.           "example": 100
  142.         },
  143.         "pages": {
  144.           "type": "integer",
  145.           "description": "总页数",
  146.           "example": 5
  147.         }
  148.       }
  149.     },
  150.     "Error": {
  151.       "type": "object",
  152.       "required": ["code", "message"],
  153.       "properties": {
  154.         "code": {
  155.           "type": "integer",
  156.           "description": "错误代码",
  157.           "example": 400
  158.         },
  159.         "message": {
  160.           "type": "string",
  161.           "description": "错误消息",
  162.           "example": "Invalid input data"
  163.         },
  164.         "details": {
  165.           "type": "array",
  166.           "items": {
  167.             "type": "string"
  168.           },
  169.           "description": "错误详情",
  170.           "example": ["Username is required", "Email must be a valid email address"]
  171.         }
  172.       }
  173.     }
  174.   }
  175. }
复制代码

这些数据模型展示了Swagger JSON中模式定义的各种特性:

1. 基本类型和格式:如string、integer、date-time、email、uuid等
2. 验证规则:如required、minLength、maxLength、pattern等
3. 枚举值:使用enum限制字段的可能值
4. 只读和只写字段:使用readOnly和writeOnly标记
5. 引用:使用$ref引用其他模式
6. 示例值:使用example提供示例数据

复杂数据模型

对于更复杂的场景,你可以定义嵌套对象、数组、多态类型等。以下是一个示例:
  1. "components": {
  2.   "schemas": {
  3.     "Order": {
  4.       "type": "object",
  5.       "required": ["id", "customer", "items", "total", "status"],
  6.       "properties": {
  7.         "id": {
  8.           "type": "string",
  9.           "format": "uuid",
  10.           "description": "订单ID",
  11.           "readOnly": true
  12.         },
  13.         "customer": {
  14.           "$ref": "#/components/schemas/Customer"
  15.         },
  16.         "items": {
  17.           "type": "array",
  18.           "description": "订单项目",
  19.           "items": {
  20.             "$ref": "#/components/schemas/OrderItem"
  21.           }
  22.         },
  23.         "shippingAddress": {
  24.           "oneOf": [
  25.             {
  26.               "$ref": "#/components/schemas/Address"
  27.             },
  28.             {
  29.               "type": "null"
  30.             }
  31.           ],
  32.           "description": "配送地址"
  33.         },
  34.         "billingAddress": {
  35.           "$ref": "#/components/schemas/Address"
  36.         },
  37.         "payment": {
  38.           "oneOf": [
  39.             {
  40.               "$ref": "#/components/schemas/CreditCardPayment"
  41.             },
  42.             {
  43.               "$ref": "#/components/schemas/PayPalPayment"
  44.             }
  45.           ],
  46.           "discriminator": {
  47.             "propertyName": "type",
  48.             "mapping": {
  49.               "credit_card": "#/components/schemas/CreditCardPayment",
  50.               "paypal": "#/components/schemas/PayPalPayment"
  51.             }
  52.           }
  53.         },
  54.         "total": {
  55.           "type": "number",
  56.           "format": "decimal",
  57.           "description": "订单总金额",
  58.           "minimum": 0
  59.         },
  60.         "status": {
  61.           "type": "string",
  62.           "description": "订单状态",
  63.           "enum": ["pending", "processing", "shipped", "delivered", "cancelled"],
  64.           "default": "pending"
  65.         },
  66.         "createdAt": {
  67.           "type": "string",
  68.           "format": "date-time",
  69.           "description": "创建时间",
  70.           "readOnly": true
  71.         },
  72.         "updatedAt": {
  73.           "type": "string",
  74.           "format": "date-time",
  75.           "description": "更新时间",
  76.           "readOnly": true
  77.         }
  78.       }
  79.     },
  80.     "OrderItem": {
  81.       "type": "object",
  82.       "required": ["product", "quantity", "price"],
  83.       "properties": {
  84.         "product": {
  85.           "$ref": "#/components/schemas/Product"
  86.         },
  87.         "quantity": {
  88.           "type": "integer",
  89.           "description": "数量",
  90.           "minimum": 1
  91.         },
  92.         "price": {
  93.           "type": "number",
  94.           "format": "decimal",
  95.           "description": "单价",
  96.           "minimum": 0
  97.         }
  98.       }
  99.     },
  100.     "Customer": {
  101.       "type": "object",
  102.       "required": ["id", "name", "email"],
  103.       "properties": {
  104.         "id": {
  105.           "type": "string",
  106.           "format": "uuid",
  107.           "description": "客户ID"
  108.         },
  109.         "name": {
  110.           "type": "string",
  111.           "description": "客户姓名"
  112.         },
  113.         "email": {
  114.           "type": "string",
  115.           "format": "email",
  116.           "description": "客户邮箱"
  117.         },
  118.         "phone": {
  119.           "type": "string",
  120.           "description": "客户电话"
  121.         }
  122.       }
  123.     },
  124.     "Address": {
  125.       "type": "object",
  126.       "required": ["street", "city", "country"],
  127.       "properties": {
  128.         "street": {
  129.           "type": "string",
  130.           "description": "街道地址"
  131.         },
  132.         "city": {
  133.           "type": "string",
  134.           "description": "城市"
  135.         },
  136.         "state": {
  137.           "type": "string",
  138.           "description": "州/省"
  139.         },
  140.         "postalCode": {
  141.           "type": "string",
  142.           "description": "邮政编码"
  143.         },
  144.         "country": {
  145.           "type": "string",
  146.           "description": "国家"
  147.         }
  148.       }
  149.     },
  150.     "CreditCardPayment": {
  151.       "type": "object",
  152.       "required": ["type", "cardNumber", "expiryDate", "cvv"],
  153.       "properties": {
  154.         "type": {
  155.           "type": "string",
  156.           "enum": ["credit_card"]
  157.         },
  158.         "cardNumber": {
  159.           "type": "string",
  160.           "description": "信用卡号",
  161.           "pattern": "^[0-9]{16}$",
  162.           "writeOnly": true
  163.         },
  164.         "expiryDate": {
  165.           "type": "string",
  166.           "description": "有效期",
  167.           "pattern": "^[0-9]{2}/[0-9]{2}$",
  168.           "writeOnly": true
  169.         },
  170.         "cvv": {
  171.           "type": "string",
  172.           "description": "CVV码",
  173.           "pattern": "^[0-9]{3,4}$",
  174.           "writeOnly": true
  175.         },
  176.         "cardholderName": {
  177.           "type": "string",
  178.           "description": "持卡人姓名",
  179.           "writeOnly": true
  180.         }
  181.       }
  182.     },
  183.     "PayPalPayment": {
  184.       "type": "object",
  185.       "required": ["type", "email"],
  186.       "properties": {
  187.         "type": {
  188.           "type": "string",
  189.           "enum": ["paypal"]
  190.         },
  191.         "email": {
  192.           "type": "string",
  193.           "format": "email",
  194.           "description": "PayPal账户邮箱"
  195.         }
  196.       }
  197.     }
  198.   }
  199. }
复制代码

这个复杂示例展示了:

1. 嵌套对象:Order包含Customer、Address等嵌套对象
2. 数组:Order.items是一个OrderItem对象的数组
3. 多态类型:使用oneOf定义多种可能的类型
4. 判别器:使用discriminator区分多态类型
5. 条件字段:使用oneOf与null类型组合定义可选字段

添加安全配置

API安全是现代应用程序的关键部分。Swagger JSON提供了多种方式来描述API的安全要求。

定义安全方案

在components.securitySchemes部分定义可用的安全方案:
  1. "components": {
  2.   "securitySchemes": {
  3.     "bearerAuth": {
  4.       "type": "http",
  5.       "scheme": "bearer",
  6.       "bearerFormat": "JWT",
  7.       "description": "JWT Token认证"
  8.     },
  9.     "basicAuth": {
  10.       "type": "http",
  11.       "scheme": "basic",
  12.       "description": "基本认证"
  13.     },
  14.     "apiKey": {
  15.       "type": "apiKey",
  16.       "in": "header",
  17.       "name": "X-API-Key",
  18.       "description": "API密钥认证"
  19.     },
  20.     "oauth2": {
  21.       "type": "oauth2",
  22.       "flows": {
  23.         "authorizationCode": {
  24.           "authorizationUrl": "https://example.com/oauth/authorize",
  25.           "tokenUrl": "https://example.com/oauth/token",
  26.           "scopes": {
  27.             "read": "读取权限",
  28.             "write": "写入权限"
  29.           }
  30.         },
  31.         "implicit": {
  32.           "authorizationUrl": "https://example.com/oauth/authorize",
  33.           "scopes": {
  34.             "read": "读取权限",
  35.             "write": "写入权限"
  36.           }
  37.         },
  38.         "password": {
  39.           "tokenUrl": "https://example.com/oauth/token",
  40.           "scopes": {
  41.             "read": "读取权限",
  42.             "write": "写入权限"
  43.           }
  44.         },
  45.         "clientCredentials": {
  46.           "tokenUrl": "https://example.com/oauth/token",
  47.           "scopes": {
  48.             "read": "读取权限",
  49.             "write": "写入权限"
  50.           }
  51.         }
  52.       }
  53.     }
  54.   }
  55. }
复制代码

这个示例定义了四种常见的安全方案:

1. Bearer认证:通常用于JWT令牌
2. 基本认证:用户名和密码
3. API密钥:在头部传递的API密钥
4. OAuth2:包括授权码、隐式、密码和客户端凭据流程

应用安全要求

在API操作上应用安全要求:
  1. "paths": {
  2.   "/users": {
  3.     "get": {
  4.       "summary": "获取用户列表",
  5.       "security": [
  6.         {
  7.           "bearerAuth": []
  8.         },
  9.         {
  10.           "apiKey": []
  11.         }
  12.       ],
  13.       // 其他操作定义...
  14.     },
  15.     "post": {
  16.       "summary": "创建用户",
  17.       "security": [
  18.         {
  19.           "bearerAuth": []
  20.         }
  21.       ],
  22.       // 其他操作定义...
  23.     }
  24.   },
  25.   "/public/info": {
  26.     "get": {
  27.       "summary": "获取公开信息",
  28.       // 没有security属性,表示不需要认证
  29.       // 其他操作定义...
  30.     }
  31.   }
  32. }
复制代码

在这个示例中:

• /users的GET操作支持Bearer认证或API密钥认证
• /users的POST操作只支持Bearer认证
• /public/info不需要认证

全局安全要求

你还可以在根级别定义全局安全要求,应用于所有API操作:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0"
  6.   },
  7.   "security": [
  8.     {
  9.       "bearerAuth": []
  10.     }
  11.   ],
  12.   "paths": {
  13.     // 路径定义...
  14.   }
  15. }
复制代码

然后,对于不需要认证的操作,可以使用空数组覆盖全局安全要求:
  1. "/public/info": {
  2.   "get": {
  3.     "summary": "获取公开信息",
  4.     "security": [],
  5.     // 其他操作定义...
  6.   }
  7. }
复制代码

高级配置技巧

自定义UI配置

Swagger UI可以通过swagger-ui扩展进行自定义:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0"
  6.   },
  7.   "paths": {},
  8.   "x-swagger-ui": {
  9.     "filter": true,
  10.     "displayRequestDuration": true,
  11.     "docExpansion": "none",
  12.     "defaultModelsExpandDepth": -1,
  13.     "defaultModelExpandDepth": 1,
  14.     "displayOperationId": true,
  15.     "showExtensions": true,
  16.     "showCommonExtensions": true,
  17.     "supportedSubmitMethods": ["get", "post", "put", "delete"],
  18.     "validatorUrl": "https://validator.swagger.io"
  19.   }
  20. }
复制代码

标签分组

使用标签对API操作进行分组,提高文档的可读性:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0"
  6.   },
  7.   "tags": [
  8.     {
  9.       "name": "用户管理",
  10.       "description": "用户相关的操作"
  11.     },
  12.     {
  13.       "name": "产品管理",
  14.       "description": "产品相关的操作"
  15.     },
  16.     {
  17.       "name": "订单管理",
  18.       "description": "订单相关的操作"
  19.     }
  20.   ],
  21.   "paths": {
  22.     "/users": {
  23.       "get": {
  24.         "tags": ["用户管理"],
  25.         "summary": "获取用户列表",
  26.         // 其他操作定义...
  27.       }
  28.     },
  29.     "/products": {
  30.       "get": {
  31.         "tags": ["产品管理"],
  32.         "summary": "获取产品列表",
  33.         // 其他操作定义...
  34.       }
  35.     },
  36.     "/orders": {
  37.       "get": {
  38.         "tags": ["订单管理"],
  39.         "summary": "获取订单列表",
  40.         // 其他操作定义...
  41.       }
  42.     }
  43.   }
  44. }
复制代码

外部文档引用

使用externalDocs引用外部文档:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0",
  6.     "externalDocs": {
  7.       "description": "完整文档",
  8.       "url": "https://docs.example.com"
  9.     }
  10.   },
  11.   "paths": {
  12.     "/users": {
  13.       "get": {
  14.         "summary": "获取用户列表",
  15.         "externalDocs": {
  16.           "description": "用户管理指南",
  17.           "url": "https://docs.example.com/user-management"
  18.         },
  19.         // 其他操作定义...
  20.       }
  21.     }
  22.   }
  23. }
复制代码

示例值和示例请求/响应

为参数、请求体和响应添加示例值:
  1. "paths": {
  2.   "/users": {
  3.     "post": {
  4.       "summary": "创建用户",
  5.       "requestBody": {
  6.         "required": true,
  7.         "content": {
  8.           "application/json": {
  9.             "schema": {
  10.               "$ref": "#/components/schemas/NewUser"
  11.             },
  12.             "example": {
  13.               "username": "johndoe",
  14.               "email": "john.doe@example.com",
  15.               "password": "SecurePassword123!",
  16.               "firstName": "John",
  17.               "lastName": "Doe"
  18.             }
  19.           }
  20.         }
  21.       },
  22.       "responses": {
  23.         "201": {
  24.           "description": "用户创建成功",
  25.           "content": {
  26.             "application/json": {
  27.               "schema": {
  28.                 "$ref": "#/components/schemas/User"
  29.               },
  30.               "examples": {
  31.                 "successExample": {
  32.                   "summary": "成功示例",
  33.                   "value": {
  34.                     "id": "550e8400-e29b-41d4-a716-446655440000",
  35.                     "username": "johndoe",
  36.                     "email": "john.doe@example.com",
  37.                     "firstName": "John",
  38.                     "lastName": "Doe",
  39.                     "role": "user",
  40.                     "createdAt": "2023-01-01T00:00:00Z",
  41.                     "updatedAt": "2023-01-01T00:00:00Z"
  42.                   }
  43.                 }
  44.               }
  45.             }
  46.           }
  47.         }
  48.       }
  49.     }
  50.   }
  51. }
复制代码

回调定义

对于需要回调的API(如webhooks),可以使用callbacks定义:
  1. "paths": {
  2.   "/subscriptions": {
  3.     "post": {
  4.       "summary": "创建订阅",
  5.       "requestBody": {
  6.         "required": true,
  7.         "content": {
  8.           "application/json": {
  9.             "schema": {
  10.               "$ref": "#/components/schemas/Subscription"
  11.             }
  12.           }
  13.         }
  14.       },
  15.       "responses": {
  16.         "201": {
  17.           "description": "订阅创建成功"
  18.         }
  19.       },
  20.       "callbacks": {
  21.         "eventCallback": {
  22.           "{$request.body#/callbackUrl}": {
  23.             "post": {
  24.               "summary": "事件回调",
  25.               "requestBody": {
  26.                 "required": true,
  27.                 "content": {
  28.                   "application/json": {
  29.                     "schema": {
  30.                       "$ref": "#/components/schemas/Event"
  31.                     }
  32.                   }
  33.                 }
  34.               },
  35.               "responses": {
  36.                 "200": {
  37.                   "description": "回调处理成功"
  38.                 }
  39.               }
  40.             }
  41.           }
  42.         }
  43.       }
  44.     }
  45.   }
  46. }
复制代码

常见问题与解决方案

1. 如何处理版本控制?

问题:随着API的发展,如何处理不同版本的API文档?

解决方案:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0",
  6.     "description": "这是API的v1版本文档。v2版本文档请访问:https://api.example.com/v2/docs"
  7.   },
  8.   "servers": [
  9.     {
  10.       "url": "https://api.example.com/v1",
  11.       "description": "API v1"
  12.     }
  13.   ],
  14.   "paths": {}
  15. }
复制代码

或者使用服务器变量:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0"
  6.   },
  7.   "servers": [
  8.     {
  9.       "url": "https://api.example.com/{version}",
  10.       "description": "API服务器",
  11.       "variables": {
  12.         "version": {
  13.           "default": "v1",
  14.           "enum": ["v1", "v2"],
  15.           "description": "API版本"
  16.         }
  17.       }
  18.     }
  19.   ],
  20.   "paths": {}
  21. }
复制代码

2. 如何处理分页?

问题:如何为分页API创建清晰的文档?

解决方案:
  1. "paths": {
  2.   "/users": {
  3.     "get": {
  4.       "summary": "获取用户列表",
  5.       "parameters": [
  6.         {
  7.           "name": "page",
  8.           "in": "query",
  9.           "description": "页码",
  10.           "required": false,
  11.           "schema": {
  12.             "type": "integer",
  13.             "default": 1,
  14.             "minimum": 1
  15.           }
  16.         },
  17.         {
  18.           "name": "limit",
  19.           "in": "query",
  20.           "description": "每页记录数",
  21.           "required": false,
  22.           "schema": {
  23.             "type": "integer",
  24.             "default": 20,
  25.             "minimum": 1,
  26.             "maximum": 100
  27.           }
  28.         },
  29.         {
  30.           "name": "sort",
  31.           "in": "query",
  32.           "description": "排序字段",
  33.           "required": false,
  34.           "schema": {
  35.             "type": "string",
  36.             "enum": ["id", "username", "email", "createdAt"],
  37.             "default": "id"
  38.           }
  39.         },
  40.         {
  41.           "name": "order",
  42.           "in": "query",
  43.           "description": "排序方向",
  44.           "required": false,
  45.           "schema": {
  46.             "type": "string",
  47.             "enum": ["asc", "desc"],
  48.             "default": "asc"
  49.           }
  50.         }
  51.       ],
  52.       "responses": {
  53.         "200": {
  54.           "description": "成功获取用户列表",
  55.           "content": {
  56.             "application/json": {
  57.               "schema": {
  58.                 "type": "object",
  59.                 "properties": {
  60.                   "data": {
  61.                     "type": "array",
  62.                     "items": {
  63.                       "$ref": "#/components/schemas/User"
  64.                     }
  65.                   },
  66.                   "pagination": {
  67.                     "type": "object",
  68.                     "properties": {
  69.                       "page": {
  70.                         "type": "integer",
  71.                         "example": 1
  72.                       },
  73.                       "limit": {
  74.                         "type": "integer",
  75.                         "example": 20
  76.                       },
  77.                       "total": {
  78.                         "type": "integer",
  79.                         "example": 100
  80.                       },
  81.                       "pages": {
  82.                         "type": "integer",
  83.                         "example": 5
  84.                       }
  85.                     }
  86.                   }
  87.                 }
  88.               }
  89.             }
  90.           }
  91.         }
  92.       }
  93.     }
  94.   }
  95. }
复制代码

3. 如何处理文件上传?

问题:如何为文件上传API创建文档?

解决方案:
  1. "paths": {
  2.   "/files": {
  3.     "post": {
  4.       "summary": "上传文件",
  5.       "requestBody": {
  6.         "required": true,
  7.         "content": {
  8.           "multipart/form-data": {
  9.             "schema": {
  10.               "type": "object",
  11.               "properties": {
  12.                 "file": {
  13.                   "type": "string",
  14.                   "format": "binary",
  15.                   "description": "要上传的文件"
  16.                 },
  17.                 "description": {
  18.                   "type": "string",
  19.                   "description": "文件描述"
  20.                 },
  21.                 "tags": {
  22.                   "type": "array",
  23.                   "items": {
  24.                     "type": "string"
  25.                   },
  26.                   "description": "文件标签"
  27.                 }
  28.               }
  29.             }
  30.           }
  31.         }
  32.       },
  33.       "responses": {
  34.         "201": {
  35.           "description": "文件上传成功",
  36.           "content": {
  37.             "application/json": {
  38.               "schema": {
  39.                 "$ref": "#/components/schemas/File"
  40.               }
  41.             }
  42.           }
  43.         }
  44.       }
  45.     }
  46.   }
  47. }
复制代码

4. 如何处理错误响应?

问题:如何为API错误创建一致的文档?

解决方案:

首先,定义一个通用的错误响应模式:
  1. "components": {
  2.   "schemas": {
  3.     "Error": {
  4.       "type": "object",
  5.       "required": ["code", "message"],
  6.       "properties": {
  7.         "code": {
  8.           "type": "integer",
  9.           "description": "错误代码",
  10.           "example": 400
  11.         },
  12.         "message": {
  13.           "type": "string",
  14.           "description": "错误消息",
  15.           "example": "Invalid input data"
  16.         },
  17.         "details": {
  18.           "type": "array",
  19.           "items": {
  20.             "type": "string"
  21.           },
  22.           "description": "错误详情",
  23.           "example": ["Username is required", "Email must be a valid email address"]
  24.         },
  25.         "timestamp": {
  26.           "type": "string",
  27.           "format": "date-time",
  28.           "description": "错误发生时间",
  29.           "readOnly": true
  30.         },
  31.         "path": {
  32.           "type": "string",
  33.           "description": "请求路径",
  34.           "readOnly": true
  35.         }
  36.       }
  37.     }
  38.   }
  39. }
复制代码

然后,在API操作中引用这个模式:
  1. "paths": {
  2.   "/users": {
  3.     "post": {
  4.       "summary": "创建用户",
  5.       "requestBody": {
  6.         "required": true,
  7.         "content": {
  8.           "application/json": {
  9.             "schema": {
  10.               "$ref": "#/components/schemas/NewUser"
  11.             }
  12.           }
  13.         }
  14.       },
  15.       "responses": {
  16.         "201": {
  17.           "description": "用户创建成功",
  18.           "content": {
  19.             "application/json": {
  20.               "schema": {
  21.                 "$ref": "#/components/schemas/User"
  22.               }
  23.             }
  24.           }
  25.         },
  26.         "400": {
  27.           "description": "无效的输入数据",
  28.           "content": {
  29.             "application/json": {
  30.               "schema": {
  31.                 "$ref": "#/components/schemas/Error"
  32.               },
  33.               "examples": {
  34.                 "validationError": {
  35.                   "summary": "验证错误示例",
  36.                   "value": {
  37.                     "code": 400,
  38.                     "message": "Invalid input data",
  39.                     "details": [
  40.                       "Username is required",
  41.                       "Email must be a valid email address"
  42.                     ],
  43.                     "timestamp": "2023-01-01T00:00:00Z",
  44.                     "path": "/users"
  45.                   }
  46.                 }
  47.               }
  48.             }
  49.           }
  50.         },
  51.         "409": {
  52.           "description": "用户已存在",
  53.           "content": {
  54.             "application/json": {
  55.               "schema": {
  56.                 "$ref": "#/components/schemas/Error"
  57.               },
  58.               "examples": {
  59.                 "conflictError": {
  60.                   "summary": "冲突错误示例",
  61.                   "value": {
  62.                     "code": 409,
  63.                     "message": "User already exists",
  64.                     "details": [
  65.                       "A user with the same username or email already exists"
  66.                     ],
  67.                     "timestamp": "2023-01-01T00:00:00Z",
  68.                     "path": "/users"
  69.                   }
  70.                 }
  71.               }
  72.             }
  73.           }
  74.         }
  75.       }
  76.     }
  77.   }
  78. }
复制代码

5. 如何处理复杂查询参数?

问题:如何为带有复杂查询参数的API创建文档?

解决方案:
  1. "paths": {
  2.   "/users": {
  3.     "get": {
  4.       "summary": "获取用户列表",
  5.       "parameters": [
  6.         {
  7.           "name": "filter",
  8.           "in": "query",
  9.           "description": "筛选条件",
  10.           "required": false,
  11.           "schema": {
  12.             "type": "object",
  13.             "properties": {
  14.               "role": {
  15.                 "type": "string",
  16.                 "enum": ["admin", "user", "guest"]
  17.               },
  18.               "status": {
  19.                 "type": "string",
  20.                 "enum": ["active", "inactive", "pending"]
  21.               },
  22.               "createdAt": {
  23.                 "type": "object",
  24.                 "properties": {
  25.                   "from": {
  26.                     "type": "string",
  27.                     "format": "date-time"
  28.                   },
  29.                   "to": {
  30.                     "type": "string",
  31.                     "format": "date-time"
  32.                   }
  33.                 }
  34.               }
  35.             }
  36.           },
  37.           "example": {
  38.             "role": "user",
  39.             "status": "active",
  40.             "createdAt": {
  41.               "from": "2023-01-01T00:00:00Z",
  42.               "to": "2023-01-31T23:59:59Z"
  43.             }
  44.           }
  45.         },
  46.         {
  47.           "name": "fields",
  48.           "in": "query",
  49.           "description": "返回字段",
  50.           "required": false,
  51.           "schema": {
  52.             "type": "array",
  53.             "items": {
  54.               "type": "string",
  55.               "enum": ["id", "username", "email", "firstName", "lastName", "role", "createdAt"]
  56.             }
  57.           },
  58.           "example": ["id", "username", "email"]
  59.         },
  60.         {
  61.           "name": "include",
  62.           "in": "query",
  63.           "description": "包含关联资源",
  64.           "required": false,
  65.           "schema": {
  66.             "type": "array",
  67.             "items": {
  68.               "type": "string",
  69.               "enum": ["profile", "settings", "permissions"]
  70.             }
  71.           },
  72.           "example": ["profile"]
  73.         }
  74.       ],
  75.       "responses": {
  76.         "200": {
  77.           "description": "成功获取用户列表",
  78.           "content": {
  79.             "application/json": {
  80.               "schema": {
  81.                 "type": "object",
  82.                 "properties": {
  83.                   "data": {
  84.                     "type": "array",
  85.                     "items": {
  86.                       "$ref": "#/components/schemas/User"
  87.                     }
  88.                   },
  89.                   "included": {
  90.                     "type": "array",
  91.                     "items": {
  92.                       "oneOf": [
  93.                         {
  94.                           "$ref": "#/components/schemas/Profile"
  95.                         },
  96.                         {
  97.                           "$ref": "#/components/schemas/Settings"
  98.                         },
  99.                         {
  100.                           "$ref": "#/components/schemas/Permission"
  101.                         }
  102.                       ]
  103.                     }
  104.                   }
  105.                 }
  106.               }
  107.             }
  108.           }
  109.         }
  110.       }
  111.     }
  112.   }
  113. }
复制代码

最佳实践

1. 保持一致性

在整个API文档中保持一致的命名约定、结构和格式。例如:

• 使用一致的命名约定(如camelCase或snake_case)
• 为所有错误响应使用相同的结构
• 为所有分页响应使用相同的结构
• 为所有时间字段使用相同的格式

2. 提供清晰的描述

为所有API组件提供清晰、简洁的描述:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "用户管理API",
  5.     "description": "提供用户注册、认证和管理的完整功能。\n\n## 认证\n此API使用Bearer Token(JWT)进行认证。请在请求头中包含`Authorization: Bearer <token>`。\n\n## 错误处理\nAPI使用标准HTTP状态码表示请求的成功或失败。错误响应包含以下结构:\n\n```json\n{\n  "code": 400,\n  "message": "错误描述",\n  "details": ["详细错误信息"],\n  "timestamp": "2023-01-01T00:00:00Z",\n  "path": "/api/path"\n}\n```",
  6.     "version": "1.0.0"
  7.   }
  8. }
复制代码

3. 使用示例

为所有参数、请求体和响应提供有意义的示例:
  1. "components": {
  2.   "schemas": {
  3.     "User": {
  4.       "type": "object",
  5.       "required": ["id", "username", "email"],
  6.       "properties": {
  7.         "id": {
  8.           "type": "string",
  9.           "format": "uuid",
  10.           "description": "用户唯一标识符",
  11.           "example": "550e8400-e29b-41d4-a716-446655440000"
  12.         },
  13.         "username": {
  14.           "type": "string",
  15.           "description": "用户名",
  16.           "minLength": 3,
  17.           "maxLength": 30,
  18.           "example": "johndoe"
  19.         },
  20.         "email": {
  21.           "type": "string",
  22.           "format": "email",
  23.           "description": "用户电子邮箱",
  24.           "example": "john.doe@example.com"
  25.         }
  26.       }
  27.     }
  28.   }
  29. }
复制代码

4. 使用引用

使用$ref引用重复使用的组件,减少冗余并提高可维护性:
  1. "components": {
  2.   "schemas": {
  3.     "User": {
  4.       "type": "object",
  5.       "properties": {
  6.         "id": {
  7.           "type": "string",
  8.           "format": "uuid"
  9.         },
  10.         "username": {
  11.           "type": "string"
  12.         },
  13.         "email": {
  14.           "type": "string",
  15.           "format": "email"
  16.         }
  17.       }
  18.     },
  19.     "NewUser": {
  20.       "allOf": [
  21.         {
  22.           "type": "object",
  23.           "required": ["username", "email", "password"],
  24.           "properties": {
  25.             "password": {
  26.               "type": "string",
  27.               "format": "password",
  28.               "minLength": 8
  29.             }
  30.           }
  31.         },
  32.         {
  33.           "$ref": "#/components/schemas/User"
  34.         }
  35.       ]
  36.     }
  37.   }
  38. }
复制代码

5. 验证你的Swagger JSON

使用Swagger编辑器或其他验证工具确保你的Swagger JSON有效:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0"
  6.   },
  7.   "paths": {}
  8. }
复制代码

6. 使用扩展

使用OpenAPI扩展添加自定义元数据:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "示例API",
  5.     "version": "1.0.0",
  6.     "x-api-id": "12345",
  7.     "x-status": "active",
  8.     "x-contact": {
  9.       "name": "API团队",
  10.       "email": "api@example.com"
  11.     }
  12.   },
  13.   "paths": {
  14.     "/users": {
  15.       "get": {
  16.         "summary": "获取用户列表",
  17.         "x-rate-limit": "100/hour",
  18.         "x-permission": "users:read",
  19.         // 其他操作定义...
  20.       }
  21.     }
  22.   }
  23. }
复制代码

7. 组织大型API文档

对于大型API,考虑将文档拆分为多个文件,然后使用$ref引用它们:
  1. {
  2.   "openapi": "3.0.0",
  3.   "info": {
  4.     "title": "大型API",
  5.     "version": "1.0.0"
  6.   },
  7.   "paths": {
  8.     "/users": {
  9.       "$ref": "paths/users.json"
  10.     },
  11.     "/products": {
  12.       "$ref": "paths/products.json"
  13.     },
  14.     "/orders": {
  15.       "$ref": "paths/orders.json"
  16.     }
  17.   },
  18.   "components": {
  19.     "schemas": {
  20.       "User": {
  21.         "$ref": "schemas/User.json"
  22.       },
  23.       "Product": {
  24.         "$ref": "schemas/Product.json"
  25.       },
  26.       "Order": {
  27.         "$ref": "schemas/Order.json"
  28.       }
  29.     },
  30.     "securitySchemes": {
  31.       "bearerAuth": {
  32.         "$ref": "security/bearerAuth.json"
  33.       }
  34.     }
  35.   }
  36. }
复制代码

总结

通过本教程,我们深入探讨了Swagger JSON配置的各个方面,从基础结构到高级技巧。我们学习了如何:

1. 创建基本的Swagger JSON配置
2. 定义API路径和操作
3. 创建和引用数据模型
4. 实现安全配置
5. 处理常见API场景,如分页、文件上传和错误处理
6. 应用最佳实践来创建专业、可维护的API文档

Swagger JSON配置是创建专业API文档的关键工具。通过遵循本教程中的指南和示例,你可以创建出清晰、全面且易于使用的API文档,帮助开发者快速理解和使用你的API。

记住,好的API文档不仅仅是技术规范,它是开发者与你的服务交互的桥梁。投入时间和精力创建高质量的API文档,将大大提高你的API的采用率和开发者满意度。

现在,你已经掌握了Swagger JSON配置的核心概念和技巧,可以开始创建自己的专业API文档了!
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Discord频道

加入Discord频道

加入QQ社群

加入QQ社群

联系我们|小黑屋|TG频道|RSS |网站地图

Powered by Pixtech

© 2025-2026 Pixtech Team.