简体   繁体   English

REST API设计:我们何时应该在Uri中使用关联资源?

[英]REST API Design: When should we use association in Uri for the resources?

We have simple e-commerce website where we have several products. 我们有一个简单的电子商务网站,其中有几种产品。 Currently, each product has "Place order" button. 当前,每个产品都有“下订单”按钮。

When user clicks on this button, we show user a form to fill Name, Mobile number and address. 当用户单击此按钮时,我们会向用户显示一个表格,以填写姓名,手机号码和地址。 We don't support any monetary transaction. 我们不支持任何货币交易。 Once user fills this form, the order is saved to database. 用户填写此表单后,订单将保存到数据库。 The order table has OrderId, ProductId, UserName, UserMobile. 订单表具有OrderId,ProductId,UserName,UserMobile。

We are designing API to save the user order. 我们正在设计API以保存用户订单。 Should we have association b/w product and order while designing this? 在设计此商品时,我们应该拥有黑白产品和订单的关联吗?

For example URI to save the user order should be like: 例如,用于保存用户订单的URI应该类似于:

  1. POST /api/products/1/lead/ - The request body has user information ie name,mobile,address. POST / api / products / 1 / lead /-请求主体具有用户信息,即名称,手机,地址。 OR 要么
  2. POST /api/lead/ - The request body has "PRODUCT ID" and user information ie name,mobile,address. POST / api / lead /-请求正文具有“产品ID”和用户信息,即名称,手机,地址。

I am confused whether productId should be in request URI or in the request body? 我对productId应该在请求URI中还是在请求正文中感到困惑? How do we make such decision? 我们如何做出这样的决定?

Given that 鉴于

  • you're first navigating to a product, before actually placing the order 您在实际下订单之前先导航到产品
  • the product id has nothing in common with the UserInformation model that you're posting 产品ID与您要发布的UserInformation模型没有共同之处

I'd go with the first option: POST /api/products/1/lead/ 我会选择第一个选项: POST /api/products/1/lead/

I would always go with a more shallow route for representing resources, just for the sake of simplicity. 为了简单起见,我总是会采用更浅层的方法来表示资源。 No, a nested route isn't complicated or anything, but I've seen nesting go really far. 不,嵌套的路线并不复杂,但是我已经看到嵌套的意义很深。 So I would keep it as shallow as possible unless... 因此,除非...

1) You plan on having more than one thing that can have a lead. 1)您计划拥有一件以上可以带来线索的东西。 For example, you can have a lead on a product: 例如,您可以在产品上获得潜在客户:

api/products/1/lead

or a lead on a managed service that you all provide or something (I'm reaching right now): 或你们所有人提供的托管服务的线索或其他东西(我现在正在联系):

api/managed_services/2/lead

You could pass that info in the body always, but I imagine it would become a little cumbersome to base what resource to create based on what properties were defined in the json. 您可以始终在主体中传递该信息,但是我想根据json中定义的属性来创建要创建的资源会有些麻烦。

2) You plan on breaking out that route and having it go to a different service eventually. 2)您计划突破该路线,并最终将其转到其他服务。 Maybe this app will have to scale substantially and a ton of users will be hitting this route moreso than any other endpoint in the system. 也许这个应用程序将不得不进行大规模扩展,并且大量用户将比系统中的任何其他端点更喜欢使用此路由。 It's a lot easier to redirect all requests to a different microservice based on the url starting with api/products than it would be redirect based on the request body. 基于api/products开头的url将所有请求重定向到其他微服务要比基于请求正文重定向要容易得多。

But honestly, I don't think it matters too much. 但老实说,我认为这并不重要。 As long as it's easy for your clients to consume. 只要您的客户容易消费。

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

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