码迷,mamicode.com
首页 > 系统相关 > 详细

swagger 部署(Mac )

时间:2019-07-04 09:36:40      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:ted   alt   host   ntb   serve   tag   ret   original   containe   

安装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;
    }

swagger 部署(Mac )

标签:ted   alt   host   ntb   serve   tag   ret   original   containe   

原文地址:https://www.cnblogs.com/jackey2015/p/11130193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!