swagger 部署(Mac )

Posted jackey2015

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    

以上是关于swagger 部署(Mac )的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot中部署Swagger2和Swagger-UI

为harbor部署swagger-ui

解决 Nginx 部署,无法访问 Swagger 文档的问题

解决 Nginx 部署,无法访问 Swagger 文档的问题

错误:“您的部署没有关联的 swagger.json” - 流分析作业上的 ACI 部署

如何在IDE部署springboot项目(有swagger和无swagger都是一样的)到服务器或者虚拟机上的docker