更改swagger-ui的basePath

时间:2016-03-30 15:16:22

标签: swagger swagger-ui

我使用swagger project create来为节点服务提供API。我跟着these instructions。我也在使用招摇工具为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的基本路径?

2 个答案:

答案 0 :(得分:1)

我基本上this guidance找到了问题的答案。

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

'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/

答案 1 :(得分:0)

使用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)