以 RESTful 方式表示对关联执行操作的最佳方式?
假设我有一个“用户”资源和一个“组”资源。它们的 RESTful 表示如下:
/user/:id
/group/:id
但是如果我想对两个资源之间的关联进行操作怎么办?例如,如果我想指定用户 #1 想要加入组 #1。我如何表示应该在用户#1 上执行组#1 的group#join?
Say I have a "user" resource and a "group" resource. RESTful representations of them would look like;
/user/:id
/group/:id
But what if I want to perform an operation on the association between the two resources? For example, if I wanted to specify that user #1 wanted to join group #1. How would I represent that group#join of group #1 should be executed on user #1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会对 /groups/1/members 或 /users/1/memberships 之类的内容进行 POST。如果您希望允许用户一次加入多个组,那么提供一个替代方案(例如对 /users/1/memberships 进行 PUT)可能是有意义的。
您有多种选择,但这取决于您想要实现的工作流程。我认为暂时忘记 REST 会有所帮助。考虑构建一个简单的浏览器应用程序。链接和表格在哪里? Web 应用程序中的每个页面都是资源的 HTML 表示形式。它有链接和表格。将其替换为其他表示形式,但保留链接和表格。
一旦您完成了这样的设计练习,您将能够更好地确定 POST 到 /groups/1/members 是否有意义。
约翰
I would probably do a POST to something like /groups/1/members or /users/1/memberships. If you want to allow a user to join multiple groups at once, it might make sense to provide an alternative like a PUT to /users/1/memberships.
You have a number of options, but it comes down to the workflow you want to achieve. I think it helps to forget about REST for a little while. Think about building a simple browser app. Where are the links and forms? Each page in your web app is an HTML representation of a resource. It has links and forms. Replace that with alternative representations, but keep the links and forms.
Once you've gone through a design exercise like that, you'll be in a better position to decide whether a POST to /groups/1/members makes sense.
John
嗯.. /group/:id 包含什么?
我假设它会在某个地方包含一组用户?
/group/:id/users/:id - 代表我的组成员身份。
迈克·布朗
Well.. What does /group/:id contain?
Im presuming it will contain a collection of users somewhere?
/group/:id/users/:id - would represent my membership to the group.
Mike Brown