You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.0 KiB
1.0 KiB
Design: 后端 API 规范化设计
API 设计规范
RESTful 资源命名
- 使用名词复数:/api/users, /api/books
- 使用 HTTP 方法:GET(查询)、POST(创建)、PUT(更新)、DELETE(删除)
- 子资源:/api/users/{id}/roles
统一响应格式
public class ApiResponse<T> {
private int code;
private String msg;
private T data;
private long timestamp;
}
分页响应格式
public class PageResponse<T> {
private int code;
private String msg;
private long total;
private List<T> rows;
}
错误码规范
- 200: 成功
- 400: 请求参数错误
- 401: 未授权
- 403: 无权限
- 404: 资源不存在
- 500: 服务器内部错误
- 业务错误码:1000-9999
架构优化
Controller 层
- 只负责参数校验和响应
- 业务逻辑委托给 Service
- 统一异常处理
Service 层
- 接口与实现分离
- 事务管理
- 缓存策略
性能优化
- 添加 Redis 缓存
- 优化数据库查询
- 批量操作优化