|
| 1 | +# Swagger 示例 |
| 2 | +- [OpenAPI 和 Swagger](#OpenAPI-和-Swagger) |
| 3 | + * [OpenAPI](#OpenAPI) |
| 4 | + * [Swagger](#Swagger) |
| 5 | + * [为什么要使用OpenAPI](#为什么要使用OpenAPI) |
| 6 | +- [Swagger规范介绍](#Swagger规范介绍) |
| 7 | + * [基本结构](#基本结构) |
| 8 | + * [元数据](#元数据) |
| 9 | + * [基本URL](#基本URL) |
| 10 | + * [消费和生产](#消费和生产) |
| 11 | + * [路径](#路径) |
| 12 | + * [参数](#参数) |
| 13 | + * [响应](#响应) |
| 14 | + * [输入和输出模型](#输入和输出模型) |
| 15 | + * [认证](#认证) |
| 16 | +- [Swagger工具箱](#Swagger工具箱) |
| 17 | + * [Swagger Editor](#Swagger-Editor) |
| 18 | + * [Swagger UI](#Swagger-UI) |
| 19 | + * [Swagger Codegen](#Swagger-Codegen) |
| 20 | + * [asciidoctor](#asciidoctor) |
| 21 | +- [如何开始](#如何开始) |
| 22 | + |
| 23 | + |
| 24 | +## OpenAPI 和 Swagger |
| 25 | +### OpenAPI |
| 26 | +OpenAPI规范(以前叫Swagger规范)是Linux基金会的一个项目,试图通过定义一种用来描述API格式或API定义的语言,来规范RESTful服务开发过程。OpenAPI规范帮助我们描述一个API的全部信息,比如: |
| 27 | + |
| 28 | +* 可用路径(```/users```)和每个路径上的可用操作(```GET /users```, ```POST /users```) |
| 29 | +* 每个操作的输入/输出参数 |
| 30 | +* 安全认证方法 |
| 31 | +* 联系信息、许可证、使用条款和其他信息 |
| 32 | + |
| 33 | +我们可以选择使用JSON或者YAML的语言格式来编写API文档, 这两种格式对人和机器都是可读的 |
| 34 | + |
| 35 | +### Swagger |
| 36 | + |
| 37 | +Swagger是围绕OpenAPI规范建立的一系列工具,可以帮我们设计、构建、编写文档和消费REST APIs |
| 38 | + |
| 39 | +主要工具包括: |
| 40 | +* Swagger Editor – 基于浏览器的编写OpenAPI文档的工具 |
| 41 | +* Swagger UI – 把OpenAPI文档转换成交互式文档的工具 |
| 42 | +* Swagger Codegen – 从OpenAPI文档生成服务器和客户端代码 |
| 43 | + |
| 44 | +### 为什么要使用OpenAPI |
| 45 | +API描述自己结构的能力是OpenAPI中所有卓越功能的根源。一旦写好OpenAPI文档,OpenAPI规范和Swagger工具可以通过各种方式进一步驱动API开发: |
| 46 | + |
| 47 | +* 对于先有设计的用户:使用Swagger Codegen为API来生成服务器存根。唯一剩下的就是实现服务器逻辑 - API已经准备好了! |
| 48 | +* 使用Swagger Codegen,可以为40多种语言的API 生成客户端。 |
| 49 | +* 使用Swagger UI生成交互式API文档,让用户直接在浏览器中尝试API调用。 |
| 50 | +* 使用OpenAPI规范文档将API相关工具连接到API。例如,将规范导入到SoapUI中,为API创建自动测试。 |
| 51 | + |
| 52 | +----- |
| 53 | +----- |
| 54 | + |
| 55 | +## Swagger规范介绍 |
| 56 | + |
| 57 | +### **基本结构** |
| 58 | +Swagger可以用JSON或YAML编写。在本例中,我们只使用YAML示例,但JSON工作效果一样好。 |
| 59 | + |
| 60 | +用YAML编写的Swagger示例示例如下所示: |
| 61 | +```yaml |
| 62 | +swagger: "2.0" |
| 63 | +info: |
| 64 | + title: Sample API |
| 65 | + description: API description in Markdown. |
| 66 | + version: 1.0.0 |
| 67 | + |
| 68 | +host: api.example.com |
| 69 | +basePath: /v1 |
| 70 | +schemes: |
| 71 | + - https |
| 72 | + |
| 73 | +paths: |
| 74 | + /users: |
| 75 | + get: |
| 76 | + summary: Returns a list of users. |
| 77 | + description: Optional extended description in Markdown. |
| 78 | + produces: |
| 79 | + - application/json |
| 80 | + responses: |
| 81 | + 200: |
| 82 | + description: OK |
| 83 | +``` |
| 84 | +
|
| 85 | +
|
| 86 | +### **元数据** |
| 87 | +每个Swagger规范文档以Swagger版本开始,2.0是最新版本。Swagger版本定义了API规范的整体结构 - 可以记录什么以及如何记录它。 |
| 88 | +
|
| 89 | +```yaml |
| 90 | +swagger: "2.0" |
| 91 | +``` |
| 92 | +
|
| 93 | +然后,需要指定```API info```,```title```(```description```可选),```version```(API的版本,而不是文件版本或Swagger版本)。 |
| 94 | + |
| 95 | +```yaml |
| 96 | +info: |
| 97 | + title: Sample API |
| 98 | + description: API description in Markdown. |
| 99 | + version: 1.0.0 |
| 100 | +``` |
| 101 | + |
| 102 | +```version``` 可以是一个任意字符串。可以使用major.minor.patch(如语义化版本控制规范),或任意格式,如1.0-beta或2016.11.15。 |
| 103 | + |
| 104 | +```description``` 支持多行和GitHub Flavored Markdown,用于丰富的文本表示。 |
| 105 | + |
| 106 | +```info``` 还支持其他字段, 比如联系信息,许可证和其他详细信息。 |
| 107 | + |
| 108 | + |
| 109 | +### **基本URL** |
| 110 | +所有API调用的基本URL由```schemes```,```host```和```basePath```定义: |
| 111 | + |
| 112 | +```yaml |
| 113 | +host: api.example.com |
| 114 | +basePath: /v1 |
| 115 | +schemes: |
| 116 | + - https |
| 117 | +``` |
| 118 | + |
| 119 | +所有API路径都是相对于基本URL。例如,/ users实际上是指*https://api.example.com/v1/users*。 |
| 120 | + |
| 121 | + |
| 122 | +### **消费和生产** |
| 123 | + |
| 124 | +```consumes``` 与 ```produces``` 部分定义API所支持的MIME类型。根级别定义可以在各个操作中被覆盖。 |
| 125 | + |
| 126 | +```yaml |
| 127 | +consumes: |
| 128 | + - application/json |
| 129 | + - application/xml |
| 130 | +produces: |
| 131 | + - application/json |
| 132 | + - application/xml |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | +### **路径** |
| 137 | +该```paths```部分定义了API中的各个端点(路径)以及这些端点支持的HTTP方法(操作)。例如,```GET /users```可以描述为: |
| 138 | +```yaml |
| 139 | +paths: |
| 140 | + /users: |
| 141 | + get: |
| 142 | + summary: Returns a list of users. |
| 143 | + description: Optional extended description in Markdown. |
| 144 | + produces: |
| 145 | + - application/json |
| 146 | + responses: |
| 147 | + 200: |
| 148 | + description: OK |
| 149 | + |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | +### **参数** |
| 154 | +操作可以包含参数,可以通过URL path(```/users/{userId}```),query string(```/users?role=admin```),headers(```X-CustomHeader: |
| 155 | +Value```)和请求体传递的参数。可以定义参数类型,格式,是否需要或可选,以及其他详细信息: |
| 156 | +```yaml |
| 157 | +paths: |
| 158 | + /users/{userId}: |
| 159 | + get: |
| 160 | + summary: Returns a user by ID. |
| 161 | + parameters: |
| 162 | + - in: path |
| 163 | + name: userId |
| 164 | + required: true |
| 165 | + type: integer |
| 166 | + minimum: 1 |
| 167 | + description: Parameter description in Markdown. |
| 168 | + responses: |
| 169 | + 200: |
| 170 | + description: OK |
| 171 | +``` |
| 172 | + |
| 173 | + |
| 174 | +### **响应** |
| 175 | +对于每个操作,可以定义可能的状态代码,例如200 OK或404 Not Found,以及```schema```响应内容。响应内容可以通过内联定义或从外部定义引用```$ref```。还可以为不同的内容类型提供示例响应。 |
| 176 | +```yaml |
| 177 | +paths: |
| 178 | + /users/{userId}: |
| 179 | + get: |
| 180 | + summary: Returns a user by ID. |
| 181 | + parameters: |
| 182 | + - in: path |
| 183 | + name: userId |
| 184 | + required: true |
| 185 | + type: integer |
| 186 | + minimum: 1 |
| 187 | + description: The ID of the user to return. |
| 188 | + responses: |
| 189 | + 200: |
| 190 | + description: A User object. |
| 191 | + schema: |
| 192 | + type: object |
| 193 | + properties: |
| 194 | + id: |
| 195 | + type: integer |
| 196 | + example: 4 |
| 197 | + name: |
| 198 | + type: string |
| 199 | + example: Arthur Dent |
| 200 | + 400: |
| 201 | + description: The specified user ID is invalid (e.g. not a number). |
| 202 | + 404: |
| 203 | + description: A user with the specified ID was not found. |
| 204 | + default: |
| 205 | + description: Unexpected error |
| 206 | +``` |
| 207 | + |
| 208 | + |
| 209 | +### **输入和输出模型** |
| 210 | +全局```definitions```部分允许定义API中使用的公共数据结构。每当```schema```需要时,可以使用```$ref```引用它们,无论是请求体和响应体。 |
| 211 | + |
| 212 | +例如,这个JSON对象: |
| 213 | +```json |
| 214 | +{ |
| 215 | + "id": 4, |
| 216 | + "name": "Arthur Dent" |
| 217 | +} |
| 218 | +``` |
| 219 | +可以表示为: |
| 220 | +```yaml |
| 221 | +definitions: |
| 222 | + User: |
| 223 | + properties: |
| 224 | + id: |
| 225 | + type: integer |
| 226 | + name: |
| 227 | + type: string |
| 228 | + # Both properties are required |
| 229 | + required: |
| 230 | + - id |
| 231 | + - name |
| 232 | +
|
| 233 | +``` |
| 234 | + |
| 235 | +然后在request body和response body中引用如下: |
| 236 | + |
| 237 | +```yaml |
| 238 | +paths: |
| 239 | + /users/{userId}: |
| 240 | + get: |
| 241 | + summary: Returns a user by ID. |
| 242 | + parameters: |
| 243 | + - in: path |
| 244 | + name: userId |
| 245 | + required: true |
| 246 | + type: integer |
| 247 | + responses: |
| 248 | + 200: |
| 249 | + description: OK |
| 250 | + schema: |
| 251 | + $ref: '#/definitions/User' |
| 252 | + /users: |
| 253 | + post: |
| 254 | + summary: Creates a new user. |
| 255 | + parameters: |
| 256 | + - in: body |
| 257 | + name: user |
| 258 | + schema: |
| 259 | + $ref: '#/definitions/User' |
| 260 | + responses: |
| 261 | + 200: |
| 262 | + description: OK |
| 263 | +``` |
| 264 | + |
| 265 | + |
| 266 | +### **认证** |
| 267 | + |
| 268 | +```securityDefinitions``` 和 ```security``` 关键字用于描述API中使用的身份认证方式。 |
| 269 | + |
| 270 | +```yaml |
| 271 | +securityDefinitions: |
| 272 | + BasicAuth: |
| 273 | + type: basic |
| 274 | +
|
| 275 | +security: |
| 276 | + - BasicAuth: [] |
| 277 | +``` |
| 278 | + |
| 279 | + |
| 280 | +支持的认证方法有: |
| 281 | + |
| 282 | +* Basic authentication |
| 283 | +* API key (as a header or query parameter) |
| 284 | +* OAuth 2 common flows (implicit, password, application and access code) |
| 285 | + |
| 286 | + |
| 287 | +## Swagger工具箱 |
| 288 | +### **Swagger Editor** |
| 289 | +Swagger Editor是第一个开源的专注于Swagger-based APIs的编辑器, |
| 290 | +可以设计、描述、记录API。Swagger编辑器非常适合快速入门Swagger规范。它清爽,高效,并具有许多功能,可帮助设计和记录RESTful接口,开箱即用。 |
| 291 | + |
| 292 | +Swagger Editor允许在浏览器内的YAML中编辑Swagger API规范,并实时预览文档。然后可以使用完整的Swagger工具(代码生成,文档等)。 |
| 293 | + |
| 294 | +* 在任何地方运行: 编辑器可以工作在任何开发环境中,无论是在本地还是在网络中 |
| 295 | +* 智能反馈:输入时有简明反馈和错误提示以及验证Swagger-compliance的语法 |
| 296 | +* 即时可视化:当你还在定义API文档时就直观地呈现,同时与API进行交互 |
| 297 | +* 智能自动完成:通过智能自动完成来更快编写 |
| 298 | +* 完全可定制:轻松配置和自定义任何东西,从行间距到主题 |
| 299 | +* 关于构建:为API生成每种流行语言的服务器端和客户端 |
| 300 | + |
| 301 | +### **Swagger UI** |
| 302 | +Swagger UI允许任何人,无论是开发团队还是最终消费者,都可以可视化地与API资源进行交互,而无需有任何实现逻辑。它是从Swagger规范自动生成的,可视化文档使后端实现和客户端消费变得容易。 |
| 303 | + |
| 304 | +* 依赖关系:在任何环境中托管Swagger UI |
| 305 | +* 人性化:允许客户端开发人员轻松交互,并尝试API暴露的每个操作,以方便消费 |
| 306 | +* 易于导航:通过整齐分类的文档快速查找和使用资源和端点 |
| 307 | +* 所有浏览器支持:Swagger UI可以在所有主流浏览器中运行 |
| 308 | +* 完全可定制:代码完全开源,可以个性化定制和调整Swagger UI |
| 309 | + |
| 310 | +### **Swagger Codegen** |
| 311 | +使用Swagger Codegen,对于每种流行语言,可以更快地构建和改善基于Swagger定义的API。Swagger |
| 312 | +Codegen可以从Swagger规范生成服务器端和客户端SDK来简化构建过程,因此团队可以更好地关注API的实现和适配。 |
| 313 | + |
| 314 | +* 生成服务器:通过生成超过20种不同语言的样板服务器代码来消除繁琐的调整和配置 |
| 315 | +* 改善API消费:生成超过40种不同语言的客户端SDK,使客户端开发人员轻松与API集成 |
| 316 | +* 持续改进:Swagger Codegen始终使用编程世界中最新和最好的改进进行更新 |
| 317 | + |
| 318 | +### **asciidoctor** |
| 319 | +* asciidoc |
| 320 | +* asciidoctor |
| 321 | + |
| 322 | +Asciidoctor是一种用于将AsciiDoc内容转换为HTML5,DocBook 5(或4.5)和其他格式的快速文本处理器和发布工具链。Asciidoctor是用Ruby编写的,封装成RubyGem并发布到RubyGems |
| 323 | +.org。该Gem还包含在几个Linux发行版中,包括Fedora,Debian和Ubuntu。Asciidoctor是开源的,托管在GitHub上,并根据MIT许可证发布。 |
| 324 | + |
| 325 | +## 如何开始 |
| 326 | + |
| 327 | +1.install: git clone 之后, 在根目录执行: |
| 328 | + |
| 329 | +``` |
| 330 | +swagger-server/bin/install.sh |
| 331 | +``` |
| 332 | + |
| 333 | +会生成几种客户端SDK、服务器端stub和asciidoc、html文档,目录结构如下: |
| 334 | + |
| 335 | +``` |
| 336 | ++---asciidoc //asciidoc文档 |
| 337 | ++---client //自动生成的客户端 |
| 338 | +| +---go //--go语言客户端 |
| 339 | +| +---html2 //--html文档 |
| 340 | +| \---java //--java客户端 |
| 341 | ++---docs //html文档 |
| 342 | +| swagger-example.html |
| 343 | ++---server //自动生成的服务器端stub |
| 344 | +| +---jaxrs-resteasy //--使用resteasy生成jaxrs的服务器端stub |
| 345 | +| \---spring //--spring服务器端stub |
| 346 | +\---swagger-server //一个server的例子, 可以生成swagger.json |
| 347 | +``` |
| 348 | + |
| 349 | +2.运行swagger-server: |
| 350 | + |
| 351 | +``` |
| 352 | +java -jar swagger-server/target/swagger-server-${version}.jar |
| 353 | +``` |
| 354 | +
|
| 355 | +swagger.json: `http://127.0.0.1:8080/v2/api-docs` |
| 356 | +
|
| 357 | +swagger-ui: `http://127.0.0.1:8080/swagger-ui.html` |
0 commit comments