-
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()
浏览器打开