深入解析Swagger Petstore示例项目中的OpenAPI 3.0规范
2026-02-04 04:37:38作者:咎岭娴Homer
什么是Swagger Petstore示例
Swagger Petstore是OpenAPI规范中最经典的示例项目之一,它模拟了一个宠物商店的API服务。这个示例完美展示了如何使用OpenAPI 3.0规范来描述RESTful API的各种元素,包括路径、操作、参数、响应和数据结构等。
OpenAPI 3.0规范基础结构
让我们先来看这个示例的基本结构:
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"name": "MIT"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/v1"
}
],
"paths": {},
"components": {}
}
openapi: 声明使用的OpenAPI规范版本info: 包含API的元信息,如版本、标题和许可证servers: 定义API的基础URLpaths: 定义API的具体端点components: 定义可重用的组件,如数据模型
API端点详解
1. 获取宠物列表接口
"/pets": {
"get": {
"summary": "List all pets",
"operationId": "listPets",
"tags": ["pets"],
"parameters": [
{
"name": "limit",
"in": "query",
"description": "How many items to return at one time (max 100)",
"required": false,
"schema": {
"type": "integer",
"maximum": 100,
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "A paged array of pets",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": { "type": "string" }
}
},
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/Pets" }
}
}
}
}
}
}
这个GET接口展示了几个重要特性:
- 查询参数
limit的定义,包括类型、格式和最大值限制 - 分页响应设计,通过
x-next头部提供下一页链接 - 使用
$ref引用组件中定义的Pets模型
2. 创建宠物接口
"post": {
"summary": "Create a pet",
"operationId": "createPets",
"tags": ["pets"],
"requestBody": {
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/Pet" }
}
},
"required": true
},
"responses": {
"201": { "description": "Null response" }
}
}
POST接口特点:
- 定义了请求体,要求必须提供
- 请求体使用JSON格式,引用
Pet模型 - 成功响应返回201状态码
3. 获取特定宠物信息接口
"/pets/{petId}": {
"get": {
"summary": "Info for a specific pet",
"operationId": "showPetById",
"tags": ["pets"],
"parameters": [
{
"name": "petId",
"in": "path",
"required": true,
"description": "The id of the pet to retrieve",
"schema": { "type": "string" }
}
],
"responses": {
"200": {
"description": "Expected response to a valid request",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/Pet" }
}
}
}
}
}
}
这个接口展示了:
- 路径参数的定义和使用
- 如何标记参数为必需
- 成功响应返回单个
Pet对象
数据模型定义
在components/schemas部分定义了三个主要模型:
1. Pet模型
"Pet": {
"type": "object",
"required": ["id", "name"],
"properties": {
"id": { "type": "integer", "format": "int64" },
"name": { "type": "string" },
"tag": { "type": "string" }
}
}
- 必须包含
id和name字段 id是64位整数tag是可选字段
2. Pets模型
"Pets": {
"type": "array",
"maxItems": 100,
"items": { "$ref": "#/components/schemas/Pet" }
}
- 表示Pet对象的数组
- 限制最大数量为100
3. Error模型
"Error": {
"type": "object",
"required": ["code", "message"],
"properties": {
"code": { "type": "integer", "format": "int32" },
"message": { "type": "string" }
}
}
- 用于错误响应
- 必须包含错误码和消息
最佳实践分析
-
版本控制:API版本通过基础URL(
/v1)体现,这是一种常见的版本控制方式 -
分页设计:使用
limit查询参数和x-next响应头实现分页,符合RESTful设计原则 -
错误处理:所有操作都定义了默认错误响应,确保一致性
-
模型复用:通过
$ref引用组件中的模型定义,避免重复 -
操作标识:每个操作都有唯一的
operationId,方便代码生成
总结
Swagger Petstore示例是学习OpenAPI 3.0规范的绝佳教材。通过这个简单的宠物商店API,我们可以学习到:
- 如何定义API的基本信息
- 如何设计RESTful端点
- 如何描述请求参数和响应
- 如何定义和复用数据模型
- 如何处理错误情况
这个示例虽然简单,但涵盖了OpenAPI规范的大部分核心概念,是API设计入门的理想起点。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0223
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0142
uni-appA cross-platform framework using Vue.jsJavaScript09
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook04
热门内容推荐
项目优选
收起
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
470
468
deepin linux kernel
C
32
16
暂无描述
Dockerfile
780
5.09 K
Ascend Extension for PyTorch
Python
759
969
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
705
1.41 K
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
2.13 K
223
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
888
2.03 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
272
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
C
462
5.49 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.11 K
1.15 K