简体   繁体   English

REST API-如何设计Rest API?

[英]REST API - How to design rest api?

I'm little bit confused about when to create a new entity in rest. 我对何时创建一个新实体感到困惑。 I have this rest api implemented in node: 我在节点中实现了这个其余的api:

GET api/v1/services - get all services GET api/v1/services获取所有服务

GET api/v1/services/{serviceId}/suppliers - get all suppliers for service id GET api/v1/services/{serviceId}/suppliers获取所有供应商的服务ID

Now, I want to add another api for getting all suppliers, no matter which service. 现在,我想添加另一个API以获取所有供应商,无论使用哪种服务。

Does the following approach is good practice? 以下方法是否是好的做法?

GET api/v1/services/suppliers - get all suppliers GET api/v1/services/suppliers -获取所有供应商

PUT api/v1/services/suppliers/{supplierId} - edit by supplier id PUT api/v1/services/suppliers/{supplierId} -按供应商ID编辑

Or should we need to create a new suppliers entity? 还是我们需要创建一个新的供应商实体?

I hope that @wizard already found an answer for his question. 我希望@wizard已经找到他的问题的答案。 But here are my thoughts. 但是,这是我的想法。

As per my understanding, the resource "api/v1/services/suppliers" is not a right way to get back all the suppliers. 据我了解,资源“ api / v1 / services /供应商”不是收回所有供应商的正确方法。 We can use the REST Subresources to represent the relationships so that it will be more readable one. 我们可以使用REST子资源来表示这些关系,从而使其更具可读性。 But here Supplier resource can't be used outside of parent resource(Services). 但是,此处的供应商资源不能在父资源(服务)之外使用。

But in this case, we want to get all supplier details and also update the specific supplier information. 但是在这种情况下,我们希望获取所有供应商详细信息,并更新特定的供应商信息。 Hence we require a flexible API. 因此,我们需要一个灵活的API。 So we have to create another endpoint(/api/v1/suppliers) for working with supplier details. 因此,我们必须创建另一个端点(/ api / v1 / suppliers)来处理供应商详细信息。

(/api/v1/suppliers - fetch all the supplier details(GET) and /api/v1/suppliers/{supplierId} [PUT] for updating a specific supplier. (/ api / v1 / suppliers-获取所有供应商详细信息(GET)和/ api / v1 / suppliers / {supplierId} [PUT]以更新特定的供应商。

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

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