RESTful POSTS,您是否将对象 POST 到单数或复数 Uri?
这些 URI 中哪一个更“适合”接收 POST(添加产品)?是否有任何可用的最佳实践或者只是个人偏好?
/product/(单数)
或
/products/(复数)
目前我们使用 /products/?query=blah
进行搜索和 /product/{productId}/
用于 GET、PUT 和删除单个产品。
Which one of these URIs would be more 'fit' for receiving POSTs (adding product(s))? Are there any best practices available or is it just personal preference?
/product/ (singular)
or
/products/ (plural)
Currently we use /products/?query=blah
for searching and /product/{productId}/
for GETs PUTs & DELETEs of a single product.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于 POST 是“追加”操作,因此 POST 到
/products
可能更加英国化,因为您会将新产品追加到现有产品列表中。只要您对 API 中的某些内容进行了标准化,我认为就足够了。
由于 REST API 应该是超文本驱动的,因此 URI 无论如何都是相对无关紧要的。客户端应该从返回的文档中提取 URI,并在后续请求中使用这些 URI;通常,应用程序和人们不需要猜测或直观地解释 URI,因为应用程序将明确指示客户端哪些资源和 URI 可用。
Since POST is an "append" operation, it might be more Englishy to POST to
/products
, as you'd be appending a new product to the existing list of products.As long as you've standardized on something within your API, I think that's good enough.
Since REST APIs should be hypertext-driven, the URI is relatively inconsequential anyway. Clients should be pulling URIs from returned documents and using those in subsequent requests; typically applications and people aren't going to need to guess or visually interpret URIs, since the application will be explicitly instructing clients what resources and URIs are available.
通常,当您事先不知道资源的标识符时,您可以使用 POST 创建资源,而当您知道时,则使用 PUT。因此,您可以 POST 到 /products,或 PUT 到 /products/{new-id}。
使用这两种方法,您将返回 201 Created,并且使用 POST 还会返回一个 Location 标头,其中包含新创建资源的 URL(假设已成功创建)。
Typically you use POST to create a resource when you don't know the identifier of the resource in advance, and PUT when you do. So you'd POST to /products, or PUT to /products/{new-id}.
With both of these you'll return 201 Created, and with the POST additionally return a Location header containing the URL of the newly created resource (assuming it was successfully created).
在 RESTful 设计中,有一些围绕创建新资源的模式。您选择的模式很大程度上取决于谁负责为新创建的资源选择 URL。
如果客户端负责选择 URL,则客户端应 PUT 到资源的 URL。相反,如果服务器负责资源的 URL,则客户端应 POST 到“工厂”资源。通常,工厂资源是正在创建的资源的父资源,并且通常是复数的集合。
因此,就您而言,我建议使用
/products
In RESTful design, there are a few patterns around creating new resources. The pattern that you choose largely depends on who is responsible for choosing the URL for the newly created resource.
If the client is responsible for choosing the URL, then the client should PUT to the URL for the resource. In contrast, if the server is responsible for the URL for the resource then the client should POST to a "factory" resource. Typically the factory resource is the parent resource of the resource being created and is usually a collection which is pluralized.
So, in your case I would recommend using
/products
您发布或获取单一事物:单一产品。
有时您会在没有特定产品(或有查询条件)的情况下获取。但你还是用单数说。
你很少使用复数形式的名字。如果您有一个集合(产品目录),那么它就是一个目录。
You POST or GET a single thing: a single PRODUCT.
Sometimes you GET with no specific product (or with query criteria). But you still say it in the singular.
You rarely work plural forms of names. If you have a collection (a Catalog of products), it's one Catalog.
我只会发布到单数
/product
。很容易混淆这两个 URL,从而造成混淆或犯错误。I would only post to the singular
/product
. It's just too easy to mix up the two URL-s and get confused or make mistakes.正如很多人所说,你可以选择任何你喜欢的风格,只要保持一致即可,但我想指出双方的一些论点;我个人偏向于单数
支持复数资源名称:
赞成使用单数资源名称(在处理多个资源时不排除复数),
As many said, you can probably choose any style you like as long as you are consistent, however I'd like to point out some arguments on both sides; I'm personally biased towards singular
In favor of plural resource names:
In favor of singular resource names (this doesn't exclude plurals when working on multiple resources)
您可以对所有这些内容使用相同的 url,并使用 MessageContext 来确定 Web 服务的调用者想要执行的操作类型。
没有指定语言,但在 Java 中你可以做这样的事情。
这样您就可以检查发送到 Web 服务的请求类型,并根据请求方法执行适当的操作。
you could use the same url for all of them and use the MessageContext to determine what type of action the caller of the web service wanted to perform.
No language was specified but in Java you can do something like this.
That way you can check the type of request that was sent to the web service and perform the appropriate action based upon the request method.