swagger 部署(Mac )

安装swagger-edit

a.install node.js
b.install http-server

npm install -g http-server

c.download swagger-editor
d.运行查看效果

cd path/to/swagger-edit
http-servers swagger-editor

默认运行在8080 port
e.在浏览器打开窗口


 
swagger-editor.png

 
web editor.png

安装swagger-ui

a.download swagger-ui
b. mkdir a folder for swagger project, then init to node

cd path/to/swagger/project
npm init
(input some infomation) 

then you can check there is a package.json
c.copy dist folder(in swagger-ui) to swagger project
d.install express model

npm install express

there is a node_modules folder and a package-lock.json
e.create index.js under swagger project

var express = require('express'); 

var app = express(); app.use('/swagger.json', express.static('dist')); app.get('/swagger.json', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); 

f.test

node index.js
 
swagger-project.png

自定义样式

修改dist中的index.html的url,可以指定到自定义的json
default is url: "http://petstore.swagger.io/v2/swagger.json
change to ./swagger.json

the . is important

you can download example json from swagger editor.

if the host is not based on swagger url, you need to specify the host.

host:****:**   //swagger.json

修改host

if the request or response is specify model,needs a definition
definitions for response or request

运行时如果提示origin *** is not allowed by Access-Control-Allow-Origin.
需要在要获取资料的那个项目中添加header

@GetMapping("/account/tag/{tag}")
    public ResponseEntity accountByTag(@PathVariable("env") String env, @PathVariable("tag") String tag) { ..... HttpHeaders headers = getHttpHeaders(); return ResponseEntity.ok().headers(headers).body(accounts); private HttpHeaders getHttpHeaders() { HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,"*"); headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,"Origin, X-Requested-With, Content-Type, Accept"); return headers; }

转载于:https://www.cnblogs.com/jackey2015/p/11130193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值