2016-03-30 7 views
2

Ich habe verwendet Swagger-Projekt erstellen, um eine API für einen Knoten-Service zu stehen. Ich folgte these instructions. Ich benutze auch Swagger-Tools, um den Swagger-Ui zu bedienen.Change basePath für swagger-u

Ich möchte den BasePath der API und ihre Dokumentation dynamisch von '/' und '/ Docs' zu routingPath und routingPath + '/ docs' ändern.

Ich bin in der Lage, die BasePath meiner Swagger API-Spezifikation zu ändern, aber ich bin mir nicht sicher, wie Sie den BasePath der Swagger-Ui ändern. Mein Code sieht wie folgt aus:

'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')); 
    } 
}); 

Wenn ich diesen Dienst ausgeführt wird, werde ich meine API-Spezifikation bei http://localhost:3000/api/collection

Und meine docs Seite bei http://localhost:3000/docs

ich die Dokumentation dienen möchte finden Seite um http://localhost:3000/api/collection/docs

Ich habe versucht, das zu tun, indem Sie eine BasePath-Option an den SwaggerUi-Konstruktor übergeben.

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

Das hat nicht funktioniert. Wer weiß, wie man den basePath des swagger-ui konfiguriert?

Antwort

1

Ich fand die Antwort auf meine Frage folgend im Wesentlichen this guidance.

Der Schlüssel war, eine zweite Express-App (Unterpfad) hinzuzufügen und einen routingPath aus der Haupt-App zu verwenden. Hier ist der Code.

'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')); 
    } 
}); 

Jetzt sind meine docs hier zur Verfügung: http://localhost:3000/api/collection/docs/

0

Mit Feder-Boot-I

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

neu zuordnen konnte, aber immer noch ein Problem, einen Teil der doc erzeugt wird, dann ein Javascript Bug ist aufgetreten:

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)