[python3] tornado 使用swagger编写接口文档

  • python3.12

  • tornado 6.1

  • swagger 1.2.3

    pip install tornado-swagger==1.2.3

示例代码

python 复制代码
import tornado.ioloop
import tornado.web
from tornado_swagger.setup import setup_swagger


class ItemHandler(tornado.web.RequestHandler):
    def get(self, item_id):
        """
        ---
        tags:
          - Items
        summary: Get an item by ID
        description: Retrieve item details by its ID
        parameters:
          - name: item_id
            in: path
            required: true
            description: ID of the item
            type: integer
        responses:
          200:
            description: Successful operation
            schema:
              type: object
              properties:
                name:
                  type: string
                price:
                  type: number
          404:
            description: Item not found
        """
        item = {"name": "ExampleItem", "price": 99.99}
        print('item_id: ', item_id)
        if int(item_id) == 1:
            self.write(item)
        else:
            self.write({"error": "Item not found"})


def make_app():
    routes = [
        tornado.web.url(r"/item/([0-9]+)", ItemHandler),
    ]
    setup_swagger(routes, swagger_url="/api/doc",
                  description="使用 setup_swagger 设置 Swagger 文档")
    return tornado.web.Application(routes)


if __name__ == "__main__":
    app = make_app()
    app.listen(38881)
    tornado.ioloop.IOLoop.current().start()

浏览器打开

http://IP:38881/api/doc

相关推荐
愤豆5 分钟前
10-Java语言核心-JVM原理--字节码与执行引擎详解
java·jvm·python
未来转换10 分钟前
Python-web开发之Flask框架入门
前端·python·flask
Birdy_x16 分钟前
接口自动化项目实战(8):请求封装
python·自动化·测试用例
好家伙VCC31 分钟前
**发散创新:用 Rust实现数据编织(DataWrangling)的高效流式处理架构**在现
java·开发语言·python·架构·rust
编程大师哥33 分钟前
Python 爬虫
python
用户01900478326733 分钟前
Python中:可迭代对象、迭代器、生成器、生成器表达式、列表推导式
python
2401_8769075235 分钟前
《Python深度学习》
开发语言·python·深度学习
BatyTao39 分钟前
Python从零起步6-文件及操作
python
学习指针路上的小学渣41 分钟前
requests笔记
后端·python
_深海凉_42 分钟前
LeetCode热题100-反转链表
python·leetcode·链表