REST URI约定-创建资源时的资源单数或复数名称 [英] REST URI convention - Singular or plural name of resource while creating it
问题描述
我是REST的新手,我注意到在某些RESTful服务中,它们使用不同的资源URI进行更新/获取/删除和创建.如
I'm new to REST and I've observed that in some RESTful services they use different resource URI for update/get/delete and Create. Such as
- 创建-在某些地方使用 /resource 使用带有POST方法的 /resources (遵守复数)(单数)
- 更新-使用 /resource/123 和PUT方法
- 获取-将 /resource/123 与GET方法一起使用
我对此URI命名约定有点困惑.我们应该使用复数还是单数来创建资源?决定的标准是什么?
I'm little bit confused about this URI naming convention. What should we use plural or singular for resource creation? What should be the criteria while deciding that?
推荐答案
使用/resources
的前提是它表示所有"资源.如果执行GET /resources
,则可能会返回整个集合.通过发布到/resources
,您将添加到集合中.
The premise of using /resources
is that it is representing "all" resources. If you do a GET /resources
, you will likely return the entire collection. By POSTing to /resources
, you are adding to the collection.
但是,单个资源位于/resource.如果您执行GET /resource
,则很可能会出错,因为此请求没有任何意义,而/resource/123
则很有意义.
However, the individual resources are available at /resource. If you do a GET /resource
, you will likely error, as this request doesn't make any sense, whereas /resource/123
makes perfect sense.
使用/resource
代替/resources
类似于如果您正在处理文件系统和文件集合,并且/resource
是具有单独456
个文件.
Using /resource
instead of /resources
is similar to how you would do this if you were working with, say, a file system and a collection of files and /resource
is the "directory" with the individual 123
, 456
files in it.
不管对与错,都随心所欲.
Neither way is right or wrong, go with what you like best.
这篇关于REST URI约定-创建资源时的资源单数或复数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!