对此资源的正确 RESTful 调用
我目前正在为现有基础设施设计一个 RESTful 接口,但我正在努力解决一个特定的方面。
我们有一组用户,可以通过 /users
访问 该集合的元素可以在 /users/1
等处访问。
但是每个用户都有一个也需要能够访问的好友列表。 users/1/friends
会是在这里进行的平静调用吗?它没有资格成为它自己的集合,因为它只与一个用户相关。
任何帮助将不胜感激
I'm currently designing a RESTful interface to an existing infrastructure and I'm struggling with one particular aspect.
We have a collection of users which can be accessed at /users
And elements of that collection can be accessed at /users/1
etc
However each user has a list of friends which needs to be able to accessed as well. Would users/1/friends
be the restful call to make here? It doesn't qualify to be a collection of it's own since it's relates to only one user.
Any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就 RESTful 设计而言,URI 可以是您想要的任何内容。但是,为了确保您的界面可以在 RESTful 中访问,您确实需要执行以下操作:
通过添加额外的间接层,您的客户端可以发现朋友列表的链接,并且对客户端来说不再重要URI 是。您可以随时更改它。
URI 是服务器的实现细节,客户端永远不应该耦合它们。唯一的耦合应该是在 rel 和媒体类型上。
As far as RESTful design is concerned, the URI can be whatever you want. However, to ensure your interface can be accessed in a RESTfully you do need to do something like this:
By adding the extra layer of indirection, your client can discover the link for the list of friends and it no longer matters to the client what that URI is. You can change it whenever you like.
URIs are an implementation detail of the server and clients should never couple on them. The only coupling should be on rels and media types.
我想说
/user/{id}/friends
是一个合适的标识符。当然你也可以使用/users/{id}/friends
,但我个人更喜欢区分物品资源和集合资源。I'd say
/user/{id}/friends
is an appropriate identifier. Of course you can also use/users/{id}/friends
, but I personally prefer to differentiate between item resources and collection resources.我看不出你的解决方案有什么问题。作为其所代表内容的 URI,这是一个非常好的选择。
I dont see see anything wrong with your solution. Perfectly good selection as a URI for what it represents.