Linux环境下Swagger API文档格式规范
一 规范总览与版本选择
二 文件结构与必填字段规范
openapi: 3.0.0
info:
title: 示例 API
version: 1.0.0
description: API 文档规范示例
servers:
- url: http://localhost:3000/api/v1
description: 本地开发环境
paths:
/users:
get:
summary: 获取用户列表
description: 返回用户基本信息
operationId: getUsers
tags:
- 用户
parameters:
- name: page
in: query
schema:
type: integer
minimum: 1
description: 页码
required: false
responses:
'200':
description: 成功返回用户列表
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/User'
'400':
$ref: '#/components/responses/BadRequest'
components:
schemas:
User:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
example: 1
name:
type: string
example: 张三
email:
type: string
format: email
example: user@example.com
responses:
BadRequest:
description: 请求参数错误
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
schemas:
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
example: 400
message:
type: string
example: 参数校验失败
三 命名与语义规范
四 验证与本地预览
docker run -d -p 8080:8080 swaggerapi/swagger-editordocker run -d -p 8081:8080 -e SWAGGER_FILE=/app/openapi.yaml -v /path/to/openapi.yaml:/app/openapi.yaml swaggerapi/swagger-ui五 与常见框架的集成与输出规范
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
springdoc.api-docs.path=/api-docsspringdoc.swagger-ui.path=/swagger-ui<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</artifactId>
</dependency>