# fastapi.exceptions.HTTPException: 404: 未找到

- **ID:** `python/fastapi-http-exception-404-not-found`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

请求的 URL 路径与 FastAPI 应用中定义的任何路由不匹配。

## 版本兼容性

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

## 解决方案

1. **Check and correct the route definition** (95% 成功率)
   ```
   @app.get('/items/{item_id}')
async def read_item(item_id: int):
    return {'item_id': item_id}
# Access /items/123, not /items/
   ```
2. **Add a custom 404 handler for better debugging** (90% 成功率)
   ```
   @app.exception_handler(404)
async def not_found_handler(request, exc):
    return JSONResponse(status_code=404, content={'message': 'Route not found'})
   ```

## 无效尝试

- **Adding a catch-all route that returns 200** — Masks real errors and may cause unintended behavior. (60% 失败率)
- **Assuming trailing slash mismatch** — FastAPI by default does not redirect, so /users and /users/ are different. (50% 失败率)
