简体   繁体   English

REST API / URL设计用于具有复数/发布方法的资源

[英]REST api / url design for resources with pluralization / post methods

Say, for example, I had a REST api url design with "Customer" as a resource. 举例来说,我有一个以“客户”为资源的REST api url设计。

Now, I want to be able to list all customers, find a specific customer by ID or Email address 现在,我希望能够列出所有客户,通过ID或电子邮件地址查找特定客户
Also, I want to be able to add a new customer... 另外,我希望能够添加新客户...

Are these example URIs right? 这些示例URI对吗?
Or should I be doing it another way..? 还是我应该用另一种方式做..?

/customer/1234 / customer / 1234
get customer with id 1234 获得ID为1234的客户

/customer/email/john@smith.com /customer/email/john@smith.com
get customer with email address 使用电子邮件地址获取客户

[POST] [POST]
/customer/ /顾客/
creates a new customer 创建一个新客户

/customers/ /顾客/
list all customers (notice pluralization) 列出所有客户(注意复数)

In my experience RESTful APIs use a single resource path that is plural. 以我的经验,RESTful API使用单一的资源路径,该路径为复数形式。 The idea is that you have customers and when you want to interact with a customer you are interacting with the customers resource in general. 这个想法是您拥有customers并且当您想与客户互动时,通常就是在与customers资源互动。

GET /customers to get a list of customers. GET /customers以获取客户列表。

POST /customers to create a new customer. POST /customers以创建新客户。

GET /customers/:id to get a specific customer with a unique id. GET /customers/:id获取具有唯一ID的特定客户。 (Generally not an attribute of a customer like email ) (通常不是客户的属性,例如email

GET /customers?email=bob@example.com to get customers with a specific attribute value. GET /customers?email=bob@example.com以获取具有特定属性值的客户。

Apigee has a blog post that covers pluralization that you might want to read. Apigee的博客文章涵盖了您可能想要阅读的复数形式。

If I was designing this API I would start with a generic hypermedia type like HAL and design a root representation that provides me access to the various scenarios you describe using links and uri templates. 如果我正在设计此API,那么我将从HAL之类的通用超媒体类型开始,并设计一个根表示,该根表示使我能够访问您使用链接和uri模板描述的各种情况。

eg 例如

GET /api
=>
<resource>
   <link rel="urn:mycompany:customer" href=".../{id}"/>
   <link rel="urn:mycompany:customers" href="..."/>
   <link rel="urn:mycompany:customersearch" href="...{?email}"/>
</resource>

I have not filled in the actual URLs because from the perspective of the consumer of the API it really does not matter how those URLs are structured. 我没有填写实际的URL,因为从API使用者的角度来看,这些URL的结构实际上并不重要。 Do whatever is easiest in you server framework. 做您服务器框架中最简单的事情。 Don't worry if you get it wrong, you can change it later and your clients won't break because they should discover the URL from the root representation. 不必担心是否弄错了,可以稍后进行更改,并且客户不会中断,因为他们应该从根表示中发现URL。

It is fairly common to assume that POSTing a customer to a collection of customers will create a new Customer, so you could just require your consumers to use that link. 假设将客户发布到客户集合中会创建一个新客户是相当普遍的,因此您可以只要求您的消费者使用该链接。 Or if you wish to be more explicit you could define a new link relation that provides a URL for creating Customers. 或者,如果您希望更加明确,则可以定义一个新的链接关系,该关系提供用于创建客户的URL。

This is how I would do it: 这就是我要做的:

/customer/1234 / customer / 1234

customer/john@smith.com or (if email not guaranteed to be unique) customers/search?email=john@smith.com customer/john@smith.com或(如果不能保证唯一的电子邮件,则是customer/search?email=john@smith.com

[POST] /customers/ (notice the plural, same as below) [POST] / customers / (注意复数,如下所示)

[GET] /customers/ [GET] /客户/

Cheers, 干杯,

Ferenc 费伦茨

Why not be robust and enable both /customer and /customers? 为什么不健壮并同时启用/ customer和/ customers?

GET /customer/1234 or /customers/1234 retrieves the same resource. GET / customer / 1234或/ customers / 1234检索相同的资源。

GET on /customer or /customers gives a list of all customers. / customer或/ customers上的GET提供所有客户的列表。

POST to /customer or /customers creates a new customer. POST到/ customer或/ customers会创建一个新客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM