在FastAPI中定义路由和端点非常简单,只需要使用FastAPI实例的装饰器方法来定义即可。下面是一个简单的示例:
from fastapi import FastAPIapp = FastAPI()@app.get("/")def read_root(): return {"message": "Hello, World"}@app.get("/itEMS/{item_id}")def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q}在上面的示例中,@app.get("/") 和 @app.get("/items/{item_id}") 分别定义了两个路由和端点,read_root() 和 read_item() 分别作为这两个端点的处理函数。在处理函数内部可以定义相应的逻辑,返回相应的数据。


