更改 swagger-ui 的基本路径

问题描述 投票:0回答:2

我使用 swagger 项目创建来为节点服务建立 API。我遵循了这些说明。我还使用 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吗?

swagger swagger-ui
2个回答
1
投票

我基本上按照这个指导找到了我的问题的答案。

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

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


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 bug:

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)
© www.soinside.com 2019 - 2024. All rights reserved.