知识卡片

普通读书笔记卡 · 3474

动作可建模为嵌套资源

理解等级:中级

当 API 需要表达取消订单等动作时,可把动作结果视为嵌套资源:POST /orders/{order_id}/cancel 表示创建一次取消事件。这样路径仍锚定订单,POST 表达创建;这是书中的设计启发,不意味着所有动作都必须有独立持久化事件。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    原书出处

    参考来源

    - 位置:《Microservice APIs》第 4 章《Principles of REST API design》(源文件:_epub-src/OEBPS/Text/04.htm) - 结论依据:原文明确提出把动作表示为嵌套资源的启发式方案,并以取消订单时创建取消事件为例。 - 原始内容:A common heuristic is to represent actions as nested resources. For example, we can have a POST /orders/{order_id}/cancel endpoint to cancel orders. In this case, we model the order’s cancellation as creating a cancellation event.