Python Web框架-FastAPI教程-第一个例子 作者:马育民 • 2026-05-07 21:07 • 阅读:10009 # 例子 编写 main.py ```python from fastapi import FastAPI app = FastAPI() # 根路由 @app.get("/") async def root(): return {"message": "Hello FastAPI"} # 带路径参数+查询参数 @app.get("/items/{item_id}") async def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "query": q} ``` ### 启动服务 ```bash # 开发热重载 uvicorn main:app --reload # 或新版 FastAPI CLI fastapi dev main.py ``` ### 访问地址 - 接口:`http://127.0.0.1:8000` - Swagger 调试文档:`http://127.0.0.1:8000/docs` - ReDoc 规范文档:`http://127.0.0.1:8000/redoc` 原文出处:http://www.malaoshi.top/show_1GW3GkZtngAo.html