使用Swagger编写接口文档语法示例
官方openapi3.0规范访问:About Swagger Specification
1 2 3 4 5
| swagger: "2.0" info: description: "this is a api for authenticating users and binding cloud accounts." version: "1.0.0" title: "Auth api"
|
swagger: “2.0” 指定swagger的版本号,此处必须为2.0
info: 描述api文档的元数据
title:接口标题
description:接口文档描述
version:接口版本号
1 2 3 4 5 6 7
| host: "52.82.26.240:5000" basePaths: /api schemes: - http - https produces: - application/json
|
host: swagger提供测试用例的主机名,如果未设定就为当前主机,可以设置端口
basePath:定义的api的前缀,必须已/开头,测试用例的主机则为:host+bashPath
schemes:指定调用接口的协议,必须是:”http”, “https”, “ws”, “wss”.默认是http.-表示是个数组元素,即schemes接受一个数组参数
produces:声明全局后端响应返回xml数据格式,通常使用”application/json”或者”application/xml”,可以在局部中定义覆盖全局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| paths: /authenticate: post: tags: - "auth" summary: "Get a accessToken" consumes: - "application/json" produces: - "application/json" parameters: - name: "body" description: "account of fastone" required: true in: "body" schema: $ref: "#/definitions/Auth" responses: 400: description: "Invalid input" 403: description: "Invalid credential" 200: description: "Get a accessToken" default:
|
paths: 路由地址
post: 请求方法,需要是http定义的请求方法(get、post、put、patch、delete、head、option)
tags: 命名空间,也可以称为标签,方便对接口进行归类,快速过滤出相关接口
summary: 接口概要
description:接口描述
consumes:前端请求格式,即body的参数格式,通常为”application/json”、”application/xml”、”application/text”
produces:后端响应格式,通常为”application/json”、”application/xml”、”application/text”
parameters:参数
name: 参数名字
description:描述
required:是否是必填参数
in:属于哪种参数 body, header, formData, query, path,cookie
body只能有一个,body里的参数需要以model的形式通过schema放在里面
enum:枚举参数值,表示可能出现的参数
schema:描述传递值
$ref: “#/definitions/Auth”
$ref: 把model 在definitions里的Auth当作参数放入body中。
responses: 状态码
model Auth的写法,会放到yaml的最后,内容如下
1 2 3 4 5 6 7 8
| definitions: Auth: type: "string" properties: usernameOrEmail: type: "string" password: type: "string"
|
示例代码,转载于Swagger编写API文档的YAML中文示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
| swagger: '2.0'
info: title: swagger说明文档 description: 学习Swagger version: 1.0.0
host: 127.0.0.1
basePath: /api
schemes: - http - https
produces: - application/json
paths: /users: get: summary: 查询所有用户信息 description: 查询出所有用户的所有信息,用户名,别名 tags: - User responses: 200: description: 所有用户信息或者用户的集合信息 schema: type: array items: $ref: '#/definitions/User' default: description: 操作异常,执行失败.返回信息描述错误详情 schema: type: object properties: message: type: string post: description: 注册一个用户 parameters: - name: username in: formData description: 用户名,不能使用已经被注册过的 required: true type: string - name: password in: formData description: 用户登陆密码,加密传输,加密存储 required: true type: string - name: alias in: formData type: string description: 用户别名 required: false responses: 200: description: 通过返回值来标示执行结果 返回true表示执行成功 schema: type: object properties: status: type: boolean description: 是否成功 default: description: 操作异常,执行失败.返回信息描述错误详情 schema: type: object properties: message: type: string /users/{id}: get: summary: 根据用户名id查询该用户的所有信息 description: 查询出某个用户的所有信息,用户名,别名等 tags: - User parameters: - name: id in: path description: 要查询的用户的用户名,它是唯一标识 required: true type: string responses: 200: description: 所有用户信息或者用户的集合信息 schema: $ref: '#/definitions/User' default: description: 操作异常,执行失败.返回信息描述错误详情 schema: type: object properties: message: type: string delete: summary: 删除用户 description: 删除某个用户的信息,被删除的用户将无法登陆 parameters: - name: id in: path type: string required: true description: 用户的唯一标示符 tags: - User responses: 200: description: 通过返回值来标示执行结果 返回true表示执行成功 schema: type: object properties: status: type: boolean description: 是否成功 default: description: 操作异常,执行失败.返回信息描述错误详情 schema: type: object properties: message: type: string patch: summary: 用户信息修改 description: 修改用户信息(用户名别名) parameters: - name: id in: path description: 用户名,要修改的数据的唯一标识符 required: true type: string - name: alias in: formData description: 新的用户别名 required: true type: string tags: - User responses: 200: description: 通过返回值来标示执行结果 返回true表示执行成功 schema: type: object properties: status: type: boolean description: 是否成功 default: description: 操作异常,执行失败.返回信息描述错误详情 schema: type: object properties: message: type: string definitions: User: type: object properties: id: type: string description: 用户的唯一id username: type: string description: 用户名 alias: type: string description: 别名
|
参考文档:用swagger生成api的接口文档(yaml版)
查看效果:swagger.io