mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6
113 字
1 分钟
Apirouter使用
2026-03-18

APIROUTER的重要参数及含义🐧#

apirouter对象是fastapi提供的一个让多文件进行接口合并的工具

具体写法如下

from fastapi import APIRouter
router = APIRouter(
prefix="/api/v1", # 🔥 最重要:路由前缀
tags=["users"], # 🔥 API分组标签(Swagger文档用)
dependencies=[], # 全局依赖(如认证依赖)
responses={404: {"description": "Not found"}} # 统一响应模型
)

然后我们创建一个接口

@router.get("/pdd")
async def fatdog():
return {}

通过prefix的定义后,路由就会变成/api/v1/pdd

tags能让docs文档更加清晰

dependencies是路由组依赖(还有全局依赖和单一依赖!),在这个router接口创建的api都会执行这个依赖项

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

Apirouter使用
https://mizuki.mysqil.com/posts/apirouter/
作者
神秘大胖狗
发布于
2026-03-18
许可协议
None

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00