首页 > 解决方案 > 如何为没有等效 HTTP 方法的 REST URL 建模?

问题描述

我有客户,我想激活和取消他们的计划。我正在尝试尽可能地 RESTful。

执行“活动”或“暂停”的操作应该是 URI 的一部分吗?

1) 发布客户/{customerId}/ activatePlan /{planName} 2) 发布客户/{customerId}/ suspendPlan /{planName}

我的问题是激活取消都是动词或动作。它们没有任何等效的 HTTP 操作(GET、POST、PATCH 等)

我的网址很安静吗?如果没有,如何使它们充分休息。

标签: rest

解决方案


一切都是RESTful范式上的资源,这些资源是用其中一种HTTP方法(GET、、、、POSTPUT)操作的DELETE

您可以创建一个计划POST

POST customers/{customerId}/{planName}

创建计划后,我们必须激活或停用它,在这里我们有几个选择:

  1. 使用. _ _ URI在这种情况下,我们使用 PUT,因为planName资源存在(所以它是一个更新):

PUT customers/{customerId}/{planName}/activate

  1. 设置planName资源的属性(仍然是 PUT,因为它是planName资源的更新)。请求activate正文中的属性(即:或:HTTP PUTactivate=trueactivate=false)

PUT customers/{customerId}/{planName}

然后您可以使用 GET 返回planName资源的状态

GET customers/{customerId}/{planName}

如果planName要从客户中删除,则 DELETE:

GET customers/{customerId}/{planName}


推荐阅读