【问题标题】:Cannot get API specs with Flask Blueprints and Swagger UI无法使用 Flask 蓝图和 Swagger UI 获取 API 规范
【发布时间】:2014-10-18 02:28:21
【问题描述】:

因此,按照烧瓶蓝图 (https://github.com/rantav/flask-restful-swagger/blob/master/examples/blueprints.py) 使用 swagger ui 的示例,我有以下代码:

app = Flask(__name__)
test_blueprint = Blueprint('tests', __name__)
test_api = swagger.docs(restful.Api(test_blueprint), apiVersion='0.1',
                        basePath='http://localhost:5000',
                        produces=["application/json", "text/html"],
                        api_spec_url='/api/spec')

# Operation TestOp defined here
test_api.add_resource(TestOp, '/')

if __name__ == "__main__":
    app.register_blueprint(test_blueprint, url_prefix='/test')
    app.run(debug=True)

但是,当我尝试访问 api 规范文档时,无法找到该 URL。 我试过了……

localhost:5000/api/spec
localhost:5000/test_api/api/spec
localhost:5000/test_api

...所有这些都返回 404。我还尝试创建没有蓝图的应用程序,使用创建文档

swagger.docs(restful.Api(app)...)

相反。完成此操作且不涉及任何蓝图后,我可以通过

访问文档
localhost:5000/api/spec

那么,我是否使用蓝图错误地创建了我的应用程序,或者我只是没有点击正确的 URL 来访问文档?

【问题讨论】:

  • 你试过localhost:5000/tests/api/spec吗?
  • 刚刚做了 - 仍然是 404。:/

标签: python flask swagger swagger-ui flask-restful


【解决方案1】:

我知道这个线程很旧,但我今天遇到了这个确切的问题,试图将 flask-restful-swagger 与我的(有点)现代的使用蓝图的烧瓶 + python3 应用程序一起使用。同样的问题,没有错误,无论我尝试什么,都没有可用的规范。

我终于放弃了这个包(因为它似乎并没有很活跃),尽管我更喜欢这个包的标记。

我选择了 Flasgger,它似乎是最近更新的。在 10 分钟内,我启动并运行了它。代码和简短教程在这里:https://github.com/rochacbruno/flasgger

【讨论】:

  • 如果已经在使用 Flask,几乎可以轻松 + 最少的代码更改
【解决方案2】:

您似乎只是没有点击正确的网址。因为您的蓝图 url_prefix 是“/test”,所以 Swagger 规范 url 应该位于:

localhost:5000/test/api/spec

【讨论】:

    【解决方案3】:

    swagger.docs() 中有问题

    from flask_restful import Api
    test_blueprint = Blueprint('tests', __name__)
    test_api = swagger.docs(Api(test_blueprint)...)
    app.register_blueprint(test_blueprint)
    

    还有什么

    main_blueprint = Blueprint('api', __name__, url_prefix='/demo')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多