【问题标题】:Change basePath for swagger-ui更改 swagger-ui 的 basePath
【发布时间】:2016-07-18 16:53:04
【问题描述】:

我使用 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 吗?

【问题讨论】:

    标签: swagger swagger-ui


    【解决方案1】:

    我基本上在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/

    【讨论】:

      【解决方案2】:

      使用 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");
          }
      }
      

      但是还是有问题,生成了部分doc, 然后发生了一个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)
      

      【讨论】:

        猜你喜欢
        • 2016-07-11
        • 1970-01-01
        • 2019-02-18
        • 2016-11-25
        • 2021-06-13
        • 2020-05-20
        • 2017-01-27
        • 2018-11-15
        • 1970-01-01
        相关资源
        最近更新 更多