无法通过Flask蓝图和Swagger UI获取API规范
根据使用 Flask 蓝图和 Swagger UI 的示例(https://github.com/rantav/flask-restful-swagger/blob/master/examples/blueprints.py),我写了以下代码:
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 规格文档时,发现这个网址无法找到。我试过...
localhost:5000/api/spec
localhost:5000/test_api/api/spec
localhost:5000/test_api
...所有这些都返回了 404 错误。我还尝试过不使用蓝图来创建应用,并用
swagger.docs(restful.Api(app)...)
来生成文档。当这样做且不涉及蓝图时,我可以在
localhost:5000/api/spec
访问文档。那么,我是把应用创建得不对吗,还是我没有找到正确的网址来访问文档呢?
3 个回答
-1
在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')
4
我知道这个帖子有点旧,但今天我遇到了完全一样的问题,想在我的(有点)现代的 Flask + Python3 应用中使用 flask-restful-swagger,这个应用还用了蓝图。问题一样,没有错误,就是不管我怎么尝试,都没有可用的规范。
最后我放弃了这个包(因为看起来它也没怎么更新过),虽然我更喜欢用这个包的标记方式。
我选择了 Flasgger,它似乎最近更新得更多。花了我10分钟就把它搞定了。代码和简短的教程在这里:https://github.com/rochacbruno/flasgger
4
看起来你访问的链接不太对。因为你的蓝图的前缀是 "/test",所以Swagger的规范链接应该是:
localhost:5000/test/api/spec