RESTful 服务中部分更新的最佳实践 - Best practice for partial updates in a RESTful service

10/14 08:28
阅读数 0

问题:

I am writing a RESTful service for a customer management system and I am trying to find the best practice for updating records partially.我正在为客户管理系统编写 RESTful 服务,我正在尝试寻找部分更新记录的最佳实践。 For example, I want the caller to be able to read the full record with a GET request.例如,我希望调用者能够通过 GET 请求读取完整记录。 But for updating it only certain operations on the record are allowed, like change the status from ENABLED to DISABLED.但是为了更新它,只允许对记录进行某些操作,例如将状态从 ENABLED 更改为 DISABLED。 (I have more complex scenarios than this) (我有比这更复杂的场景)

I don't want the caller to submit the entire record with just the updated field for security reasons (it also feels like overkill).出于安全原因,我不希望调用者仅提交带有更新字段的整个记录​​(这也感觉有点矫枉过正)。

Is there a recommended way of constructing the URIs?是否有推荐的构造 URI 的方法? When reading the REST books RPC style calls seem to be frowned upon.在阅读 REST 书籍时,RPC 风格的调用似乎不受欢迎。

If the following call returns the full customer record for the customer with the id 123如果以下调用返回 ID 为 123 的客户的完整客户记录

GET /customer/123
<customer>
    {lots of attributes}
    <status>ENABLED</status>
    {even more attributes}
</customer>

how should I update the status?我应该如何更新状态?

POST /customer/123/status
<status>DISABLED</status>

POST /customer/123/changeStatus
DISABLED

...

Update : To augment the question.更新:增加问题。 How does one incorporate 'business logic calls' into a REST api?如何将“业务逻辑调用”合并到 REST api 中? Is there an agreed way of doing this?是否有一种商定的方式来做到这一点? Not all of the methods are CRUD by nature.并非所有方法本质上都是 CRUD。 Some are more complex, like ' sendEmailToCustomer(123) ', ' mergeCustomers(123, 456) ', ' countCustomers() '有些更复杂,比如“ sendEmailToCustomer(123) ”、“ mergeCustomers(123, 456) ”、“ countCustomers()

POST /customer/123?cmd=sendEmail

POST /cmd/sendEmail?customerId=123

GET /customer/count 

解决方案:

参考一: https://en.stackoom.com/question/AFcS
参考二: https://stackoom.com/question/AFcS
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部