首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Flask Blueprints和Swagger UI获取API规范

无法使用Flask Blueprints和Swagger UI获取API规范
EN

Stack Overflow用户
提问于 2014-08-25 07:59:59
回答 3查看 4.5K关注 0票数 4

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

代码语言:javascript
复制
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。我试过..。

代码语言:javascript
复制
localhost:5000/api/spec
localhost:5000/test_api/api/spec
localhost:5000/test_api

其中的...all返回404。我还尝试过在没有蓝图的情况下创建应用程序,使用

代码语言:javascript
复制
swagger.docs(restful.Api(app)...)

而不是。当这项工作完成并且不涉及蓝图时,我可以在以下位置找到文档

代码语言:javascript
复制
localhost:5000/api/spec

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

EN

回答 3

Stack Overflow用户

发布于 2014-08-27 23:54:17

看起来你只是没有找到正确的URL。因为您的蓝图url_prefix是"/test",所以Swagger规范的url应该是:

代码语言:javascript
复制
localhost:5000/test/api/spec
票数 4
EN

Stack Overflow用户

发布于 2016-10-13 23:37:14

我知道这个帖子很老了,但是我今天遇到了这个问题,我试着把flask-restful-swagger和我(有点)使用蓝图的现代flask + python3应用程序一起使用。同样的问题,没有错误,只是无论我尝试了什么,都没有可用的规范。

我最终放弃了这个包(因为它看起来并不是很活跃),尽管我更喜欢这个包的标记。

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

票数 4
EN

Stack Overflow用户

发布于 2019-02-27 14:20:43

swagger.docs()中的一些错误

代码语言:javascript
复制
from flask_restful import Api
test_blueprint = Blueprint('tests', __name__)
test_api = swagger.docs(Api(test_blueprint)...)
app.register_blueprint(test_blueprint)

更重要的是

代码语言:javascript
复制
main_blueprint = Blueprint('api', __name__, url_prefix='/demo')
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25477430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档