【问题标题】:Changing root path of swagger bundle javascript更改 swagger bundle javascript 的根路径
【发布时间】:2020-02-12 16:52:33
【问题描述】:

我在将 Swagger 配置到我的烧瓶应用程序时遇到问题,该烧瓶应用程序部署为另一个父项目下的微服务。

如图所示,Swagger 的静态文件是这样加载的 <script src="/swaggerui/swagger-ui-bundle.js"></script>

这导致了问题,因为 .js 试图从我无法控制的父应用程序的根路径解析。

在加载这些 Javascript 时,在哪里指定 Swagger 以查找自定义路径?只需在前面添加一个点. 即可解决问题

我的代码是这样的

from flask import Flask, Blueprint
from flask_restplus import Api

app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(blueprint, doc='/doc/')
app.register_blueprint(blueprint)

【问题讨论】:

    标签: flask swagger flask-restful


    【解决方案1】:

    通过覆盖默认模板的swagger_static 并通过spec_url 传递必要的路径,我们可以改变swagger 去寻找html 和json 的路径。

    完整的解决方案如下

    @apidoc.apidoc.add_app_template_global
    def swagger_static(filename):
        return "./swaggerui/{0}".format(filename)
    
    api = Api(app)
    
    @api.documentation
    def custom_ui():
        return render_template("swagger-ui.html", title=api.title, specs_url="./swagger.json")
    

    链接Github讨论https://github.com/noirbizarre/flask-restplus/issues/262#issuecomment-542490899

    【讨论】:

      猜你喜欢
      • 2015-01-30
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2018-06-18
      • 2018-09-20
      • 2019-10-15
      相关资源
      最近更新 更多