在 Swagger UI 中更改默认 API 规范 URL [英] Change default API spec URL in Swagger UI

查看:45
本文介绍了在 Swagger UI 中更改默认 API 规范 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Swagger Editor Docker 映像来定义我的 API 规范并生成一个 nodejs 服务器以作为存根 api 运行并为我的 API 规范提供文档.

I'm using the Swagger Editor Docker image for defining my API specification and generate a nodejs server to run as stub api and serve the documentation for my API spec.

我想知道是否可以将默认 URL 更改为我自己的 URL,以便在我打开页面时显示我的规范,以及我将如何执行此操作.

I would like to know if it is possible to change the default URL to my own in order to present my spec when I open the page, and how I would do that.

比如我想把默认地址改成http://localhost:8080/spec

For instance, I would like to change the default address to http://localhost:8080/spec

推荐答案

如果我理解正确,您使用 Swagger Codegen 生成一个 Node.js 服务器.现在,当您 npm start 并打开 Swagger UI 时,您希望输入框(规范 URL)显示 http://localhost:8080/spec 而不是 http://localhost/api 文档.

If I understand correctly, you used Swagger Codegen to generate a Node.js server. Now when you npm start and open Swagger UI, you want the input box (the spec URL) to display http://localhost:8080/spec instead of http://localhost/api-docs.

要更改提供 .yaml 规范的路径,请按如下方式编辑您的 index.js:

To change the path the .yaml spec is served from, edit your index.js as follows:

// 1 - add swaggerUi options
var uiOptions = {
  apiDocs: '/spec'  // <-- override the default /api-docs
};

swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  ...

  // 2 - pass these options to swaggerUi
  app.use(middleware.swaggerUi(uiOptions));

端口号由index.js中的serverPort变量指定:

The port number is specified by the serverPort variable in index.js:

var serverPort = 8080;

这篇关于在 Swagger UI 中更改默认 API 规范 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆