REST API 最佳实践 🌟
(◕‿◕✿) 在这一章中,我们将学习设计优秀 REST API 的最佳实践和设计原则呢~ ✨
命名规范
使用名词而非动词
✅ 正确:
| GET /users
POST /users
PUT /users/123
|
❌ 错误:
| GET /getUsers
POST /createUser
PUT /updateUser/123
|
使用复数形式
| /users # 用户集合
/users/123 # 特定用户
/products # 产品集合
/orders # 订单集合
|
资源关系嵌套
| /users/123/orders # 用户的订单
/users/123/orders/456 # 用户的特定订单
/products/789/reviews # 产品的评论
|
版本管理
URI 版本控制
| /api/v1/users
/api/v2/users
|
请求头版本控制
| GET /api/users HTTP/1.1
Host: api.example.com
Accept: application/vnd.example.v1+json
|
分页和过滤
分页参数
| GET /api/users?page=1&limit=10
|
响应格式
| {
"data": [
{
"id": 1,
"name": "用户1"
}
],
"pagination": {
"page": 1,
"limit": 10,
"total": 100,
"pages": 10
}
}
|
过滤和搜索
| GET /api/users?status=active&role=admin
GET /api/users?q=keyword&sort=name&order=asc
|
错误处理
统一错误格式
| {
"error": {
"code": "VALIDATION_ERROR",
"message": "请求参数验证失败",
"details": [
{
"field": "email",
"message": "邮箱格式不正确"
}
],
"request_id": "req_123456"
}
}
|
适当的状态码
400 - 客户端请求错误 401 - 未认证 403 - 无权限 404 - 资源不存在 409 - 资源冲突 422 - 业务逻辑错误 500 - 服务器错误
安全性
使用 HTTPS
| https://api.example.com/users
|
认证和授权
| GET /api/users HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
|
速率限制
| HTTP/1.1 200 OK
X-RateLimit-Limit: 1000
X-RateLimit-Remaining: 999
X-RateLimit-Reset: 1640995200
|
数据格式
使用 JSON
| {
"id": 123,
"name": "小明",
"email": "xiaoming@example.com",
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-02T00:00:00Z"
}
|
时间格式
使用 ISO 8601 格式:
| 2024-01-01T12:00:00Z
2024-01-01T12:00:00+08:00
|
空值处理
| {
"name": "小明",
"middle_name": null,
"hobbies": []
}
|
性能优化
缓存控制
| HTTP/1.1 200 OK
Cache-Control: public, max-age=3600
ETag: "abc123"
Last-Modified: Mon, 01 Jan 2024 00:00:00 GMT
|
压缩响应
| GET /api/users HTTP/1.1
Host: api.example.com
Accept-Encoding: gzip, deflate
|
字段选择
| GET /api/users?fields=id,name,email
|
文档和测试
API 文档
使用 OpenAPI/Swagger 规范:
| openapi: 3.0.0
info:
title: 用户 API
version: 1.0.0
paths:
/users:
get:
summary: 获取用户列表
responses:
'200':
description: 成功
|
测试策略
监控和日志
日志记录
| {
"timestamp": "2024-01-01T12:00:00Z",
"level": "INFO",
"method": "GET",
"path": "/api/users",
"status": 200,
"duration": 150,
"user_id": "user_123",
"request_id": "req_456"
}
|
监控指标
最佳实践总结
遵循这些最佳实践可以让你的 API 更加:
- 易用:清晰的命名和文档
- 可靠:统一的错误处理
- 安全:适当的认证和授权
- 高效:合理的缓存和性能优化
- 可维护:良好的版本管理和监控
记住,好的 API 设计是成功项目的基础哦!(★ω★)
在下一章中,我们将学习 API 的认证和授权机制~ 🚀