rest - 如何为没有等效 HTTP 方法的 REST URL 建模?
问题描述
我有客户,我想激活和取消他们的计划。我正在尝试尽可能地 RESTful。
执行“活动”或“暂停”的操作应该是 URI 的一部分吗?
1) 发布客户/{customerId}/ activatePlan /{planName} 2) 发布客户/{customerId}/ suspendPlan /{planName}
我的问题是激活和取消都是动词或动作。它们没有任何等效的 HTTP 操作(GET、POST、PATCH 等)
我的网址很安静吗?如果没有,如何使它们充分休息。
解决方案
一切都是RESTful
范式上的资源,这些资源是用其中一种HTTP
方法(GET
、、、、POST
等PUT
)操作的DELETE
。
您可以创建一个计划POST
:
POST customers/{customerId}/{planName}
创建计划后,我们必须激活或停用它,在这里我们有几个选择:
- 使用. _ _
URI
在这种情况下,我们使用 PUT,因为planName
资源存在(所以它是一个更新):
PUT customers/{customerId}/{planName}/activate
- 设置
planName
资源的属性(仍然是 PUT,因为它是planName
资源的更新)。请求activate
正文中的属性(即:或:HTTP
PUT
activate=true
activate=false)
PUT customers/{customerId}/{planName}
然后您可以使用 GET 返回planName
资源的状态
GET customers/{customerId}/{planName}
如果planName
要从客户中删除,则 DELETE:
GET customers/{customerId}/{planName}