REST URI 约定 - 创建资源时的单数或复数名称 [英] REST URI convention - Singular or plural name of resource while creating it

查看:26
本文介绍了REST URI 约定 - 创建资源时的单数或复数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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

  • 创建 - 使用 /resources 和 POST 方法(注意复数)在某些地方使用 /resource(单数)
  • 更新 - 使用 /resource/123 和 PUT 方法
  • Get - 使用 /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,您可能会返回整个集合.通过 POST 到 /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 与使用文件系统和文件集合以及 /resources 的方式类似code>/resource 是目录",其中包含单独的 123456 文件.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆