# FastAPI 路由冲突：/items/{item_id} 与 /items/new 冲突

- **ID:** `python/fastapi-route-order-conflict`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

FastAPI 中动态路由和静态路由顺序不当，导致动态路由捕获了静态路径。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   @app.get('/items/new')
async def new_item(): ...
@app.get('/items/{item_id}')
async def get_item(item_id: int): ...
   ```
2. **** (90% 成功率)
   ```
   @app.get('/items/{item_id:\\d+}')
async def get_item(item_id: int): ...
   ```

## 无效尝试

- **** — 如果业务需要这两个路径，忽略会导致功能缺失。 (50% 失败率)
- **** — FastAPI 按定义顺序匹配，交换后可能仍冲突。 (60% 失败率)
