首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用flask-restx进行API响应的Swagger文档

使用flask-restx进行API响应的Swagger文档
EN

Stack Overflow用户
提问于 2021-09-08 14:31:20
回答 1查看 412关注 0票数 0

我正在阅读有关flask-restx的swagger文档的the documentation,并遵循以下示例。我知道为了生成API参数的swagger文档,我应该这样做。

代码语言:javascript
复制
@api.doc(params={'id': 'An ID'})

但是,我找不到如何记录API响应正文的解释。不是响应代码,而是get-method返回的结果。我要找的东西如下所示:

代码语言:javascript
复制
class MyResource(Resource):
    @api.doc(returns={"info": "Some very interesting information"})
    def get(self, id):
        res = some_function_of_id(id)
        return {"info": res}

有没有人知道这是否可能,如果可能,是如何实现的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-09 08:10:22

试用api.response装饰器

代码语言:javascript
复制
model = api.model('Model', {
    'name': fields.String,
})
@api.route('/my-resource/')
class MyResource(Resource):
    @api.response(200, 'Success', model)
    @api.response(400, 'Validation Error')
    def get(self):
        pass

请注意,@api.marshal_with()装饰器会自动记录响应。

https://flask-restx.readthedocs.io/en/latest/swagger.html#documenting-with-the-api-response-decorator

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69104988

复制
相关文章

相似问题

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