swagger-editor和swagger-ui的搭建

2018-04-12  本文已影响0人  白菜饼

系统:Ubuntu 16.04LTS

1.swagger-editor

  • 当然官方提供了在线的编辑方式,这里实现的是本地swagger-editor的使用
  • swagger-editor主要是编写api接口文档,但需要配合swagger-ui来查看,里面的代码格式为yaml,但编辑后可以导出yml/json文件
node -v
npm -v
npm install -g http-server
wget https://github.com/swagger-api/swagger-editor/releases/download/v2.10.4/swagger-editor.zip
unzip swagger-editor.zip
http-server swagger-editor

若想更换默认端口启动,增加参数

http-server –p 8081 swagger-editor

启动成功:

Starting up http-server, serving swagger-editor
Available on:
  http://127.0.0.1:8081
  http://192.168.0.79:8081
Hit CTRL-C to stop the server

使用浏览器访问http://192.168.0.79:8081即可

参考:
github源码
官方文档

2.swagger-ui

两种方式:A.扩展版,B.官方版

A.扩展版(使用Tomcat)
image.png

将编辑好的API json文档放在项目的json文件夹下(yml格式的可放在yml文件夹下,归类清晰),在界面导航栏中访问文件即可:
http://localhost:8080/swagger/yml/123.ymlhttp://localhost:8080/swagger/json/123.json

另外,sosoapi提供了表单式的编辑API方式,与swagger-editor直接编辑yml的方式相比,更简单点。

B.官方版
git clone https://github.com/swagger-api/swagger-ui
http-server –p 8082 swagger-ui
...
const ui = SwaggerUIBundle({
  //url: "http://petstore.swagger.io/v2/swagger.json",
  url: "/json/xxx.json",
dom_id: '#swagger-ui',
...
11.png

这里使用的是http-server服务器,方便快捷。也可以搭建node服务器,参考这里

Finish!

上一篇 下一篇

猜你喜欢

热点阅读