更改 swagger-ui 的 basePath

Change basePath for swagger-ui

我使用 swagger project create 为节点服务建立了 API。我关注了these instructions。我也在使用 swagger-tools 来服务 swagger-ui.

我想将 API 的 basePath 及其文档从 '/' 和 '/docs' 动态更改为 routingPath 和 routingPath + '/docs'。

我可以更改我的 swagger API 规范的 basePath,但我不确定如何更改 swagger-ui 的 basePath。我的代码如下所示:

'use strict';

const defaultRoutingPath = '/api/collection';
const defaultPort = 3000;

var path = require('path');
var SwaggerExpress = require('swagger-express-mw');
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');
var app = require('express')();
module.exports = app; // for testing

var routingPath = process.env.ROUTING_PATH || defaultRoutingPath;

var config = {
  appRoot: __dirname // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  swaggerExpress.runner.swagger.basePath = routingPath; // this works

  app.get('/', (req, res) => {
    res.redirect(path.join(routingPath, 'docs'));
  })

  // enable SwaggerUI
  app.use(swaggerExpress.runner.swaggerTools.swaggerUi({
    basePath: routingPath  // this has no effect
  }));

  // install middleware
  swaggerExpress.register(app);

  var port = process.env.PORT || defaultPort;
  app.listen(port);

  if (swaggerExpress.runner.swagger.paths['/hello']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + path.join(routingPath, '/hello?name=Scott'));
  }
});

如果我 运行 这项服务,我会在以下位置找到我的 API 规范 http://localhost:3000/api/collection

我的文档页面位于 http://localhost:3000/docs

我想在 http://localhost:3000/api/collection/docs

我试图通过将 basePath 选项传递给 swaggerUi 构造函数来做到这一点。

  // enable SwaggerUI
  app.use(swaggerExpress.runner.swaggerTools.swaggerUi({
    basePath: routingPath  // this has no effect
  }));

那没用。有人知道如何配置 swagger-ui?

的 basePath

我基本上是根据 this guidance.

找到了问题的答案

关键是添加第二个快速应用程序(子路径)并使用主应用程序中的路由路径。这是代码。

'use strict';

const defaultRoutingPath = '/api/collection';
const defaultPort = 80;

var path = require('path');
var SwaggerExpress = require('swagger-express-mw');
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');
var express = require('express');
var app = express();
var subpath = express();
module.exports = app; // for testing

var routingPath = process.env.ROUTING_PATH || defaultRoutingPath;

var config = {
  appRoot: __dirname, // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  app.use(routingPath, subpath);

  app.get('/', (req, res) => {
    res.redirect(path.join(routingPath, 'docs'));
  })

  swaggerExpress.runner.swagger.basePath = routingPath;

  // enable SwaggerUI
  subpath.use(swaggerExpress.runner.swaggerTools.swaggerUi());

  // install middleware
  swaggerExpress.register(app);

  var port = process.env.PORT || defaultPort;
  app.listen(port);

  if (swaggerExpress.runner.swagger.paths['/health']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + path.join(routingPath, '/health'));
  }
});

现在我的文档可以在这里找到: http://localhost:3000/api/collection/docs/

使用 spring-boot 我可以重新映射

public class CustomResourceMapping extends WebMvcConfigurerAdapter  { 
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController(
        "/configuration/ui", 
        "/swagger-resources/configuration/ui");
        registry.addRedirectViewController(
        "/configuration/security", 
        "/swagger-resources/configuration/security");
    }
}

但还是有问题,生成了部分文档, 然后出现 javascript 错误:

Navigated to http://localhost:8080/swagger-ui.html jquery-1.8.0.min.js:2 
XHR finished loading: GET "http://localhost:8080/configuration/ui".                 jquery-1.8.0.min.js:2 
XHR finished loading: GET "http://localhost:8080/swagger-resources".                jquery-1.8.0.min.js:2 
XHR finished loading: GET "http://localhost:8080/v2/api-docs".                      swagger-ui.min.js:10
swagger-ui.min.js:1 Uncaught TypeError: Cannot read property 'type' of undefined    swagger-ui.min.js:1
    at Object.<anonymous> (swagger-ui.min.js:1)
    at Object.10 (swagger-ui.min.js:2)
    at Object.f [as inverse] (handlebars-2.0.0.js:27)
    at Object.<anonymous> (handlebars-2.0.0.js:27)
    at Object.9 (swagger-ui.min.js:2)
    at Object.f [as inverse] (handlebars-2.0.0.js:27)
    at Object.<anonymous> (handlebars-2.0.0.js:27)
    at Object.main (swagger-ui.min.js:2)
    at e (handlebars-2.0.0.js:27)
    at s.render (swagger-ui.min.js:10)