知识卡片
空集合按请求语义响应
数据响应没有记录时,需要区分合法零匹配与请求处理失败。
状态码说明请求是否被正确处理,不能仅凭“没有数据”决定:
| 情形 | 响应含义 |
|---|---|
| 合法集合筛选零匹配 | 200与空集合 |
| 请求某个既存资源却不存在 | 404 |
| 查询参数或语法无效 | 适当4xx并提示修正 |
| 请求有效但存储或服务失败 | 适当5xx |
代理可把上游“空集合返回404”的行为转换成自己的稳定集合契约,同时隐藏内部故障细节。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 分布式数据 · 查看原书依据
In general, return 200 OK with an empty collection for any well-formed queries. Reserve 4xx and 5xx status codes for cases where the client or service encounters an error. The single-resource query that returns 404 is the exception to this rule.
参考来源
- 位置:《RESTful Web API Patterns and Practices Cookbook Connecting and Orchestrating Microservices and Distributed Data (Mike Amundsen)》第6章《分布式数据》§6.6(来源项目:book-sources;源文件:extracted/RESTful Web API Patterns and Practices Cookbook Connecting and Orchestrating Microservices and Distributed Data (Mike Amundsen)/_epub-src/OEBPS/ch06.xhtml)
- 结论依据:原文以状态码说明请求是否被正确处理,不能仅凭“没有数据”决定:
| 情形 | 响应含义 |
| --- | --- |
| 合法集合筛选零匹配 | 200与空集合 |
| 请求某个既存资源却不存在 | 404 |
| 查询参数或语法无效 | 适当4xx并提示修正 |
| 请求有效但存储或服务失败 | 适当5xx |
代理可把上游“空集合返回404”的行为转换成自己的稳定集合契约,同时隐藏内部故障细节的方式说明该用途;同节示例与讨论补充了卡内条件和边界。
- 原始内容:In general, return 200 OK with an empty collection for any well-formed queries. Reserve 4xx and 5xx status codes for cases where the client or service encounters an error. The single-resource query that returns 404 is the exception to this rule.