引入swagger主要是为了:1)作为文档。供客户端开发人员用来查阅后端API定义; 2)作为测试工具。供测试人员使用来发送HTTP请求,测试API。
1. swagger-ui工作原理
swagger-ui的使用非常简单。
可以clone其源码( https://github.com/swagger-api/swagger-ui ),其中dist目录就是完整可部署的swagger前端。
直接用浏览器打开dist目录中的index.html,页面上显示的是示例应用pet store的API。
查看index.html源码,其中有这样的代码:
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "http://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
其中url: "http://petstore.swagger.io/v2/swagger.json