简体   繁体   English

RESTful服务的url设计

[英]url design for RESTful services

I have a resource called Pricing which i want to retrieve. 我有一个名为Pricing的资源,我想要检索。 An Offer can have pricing and a Promo can have Pricing resource and there is another entity Customer with which Pricing can be mapped. Offer可以有定价, Promo可以有Pricing资源,还有另一个实体Customer可以映射Pricing I want to retrieve Pricing based on either one of OfferId / PromoId / CustomerId . 我想根据OfferId / PromoId / CustomerId之一检索Pricing

To design the URLs for this, i'm running into two options: 要为此设计URL,我遇到两个选项:

Option 1: Pass it as query string 选项1:将其作为查询字符串传递

/pricing?OfferId=234&PromoId=345&CustomerId=543234

Option 2: Have three APIs 选项2:有三个API

/pricing/offer?id=234
/pricing/promo?id=345
/pricing/customer?id=543234

IMO, OfferId / PromoId / CustomerId should be treated as attribute of the resource. OfferId IMO, OfferId / PromoId / CustomerId视为资源的属性。 Therefore pass attribute as query string.I'm more inclined towards Option 1. 因此将属性作为查询字符串传递。我更倾向于选项1。

Option 2 avoids if else condition to retrieve the resource and looks much cleaner but does it seems to be supporting REST standard of URL design? 选项2避免if else条件检索资源,看起来更干净,但它似乎支持REST设计的URL标准?

What's the REST standard to design the URL. 什么是设计URL的REST标准。 Which option would you recommend? 你会推荐哪个选项?

I prefer the Option 1. 我更喜欢选项1。
The Option 2 has the following defects: 选项2具有以下缺陷:

  1. it may confuse users. 它可能会使用户感到困惑。 for instance, /pricing/offer/234 seems to represent an Offer resource, not a Pricing resource. 例如, /pricing/offer/234似乎代表Offer资源,而不是Pricing资源。
  2. in business logic, an Offer resource contains a Pricing , but /pricing/offer/234 represent right on the contrary way. 在商业逻辑, Offer资源包含了Pricing ,但/pricing/offer/234代表权相反的方式。 It seems like a Pricing resource contains an Offer resource. 似乎Pricing资源包含Offer资源。

Actually, the Option 1, has some problems too. 实际上,选项1也存在一些问题。 for example, 例如,

/pricing?OfferId=234&PromoId=345&CustomerId=543234  

will get three Pricings, right? 会得到三个Pricings,对吗? It seems 它似乎

/pricings?OfferId=234&PromoId=345&CustomerId=543234  

is more reasonable. 更合理。

Another option you can think about is Option 3: 您可以考虑的另一个选项是选项3:

/offer/234/pricing
/promo/345/pricing
/cusomer/543234/pricing

hope it helpful. 希望它有所帮助。

The way that would be most clean and follows standard pathing would be: 最干净并遵循标准路径的方式是:

/pricing/offer/234
/pricing/promo/345
/pricing/customer/543234

With the layout being: /pricing/${offer|promo|customer|/${PathParamForId} 布局为: /pricing/${offer|promo|customer|/${PathParamForId}

Which you could then do as three separate methods one each for offer/promo/customer. 然后,您可以将其作为三个单独的方法,每个方法用于提供/促销/客户。

Then you just have to make sure your API is well documented so users know the expected behaviors for the paths. 然后,您只需确保您的API有详细记录,以便用户了解路径的预期行为。 (Difference between offer vs promo lookup and etc.) (报价与促销查询之间的差异等)

This is already suggested by @Freedom but I think it deserves its own answer and reasoning. 这已经由@Freedom提出,但我认为它应该得到自己的答案和推理。

You want to retrieve a pricing - but that doesn't mean you have to start the URL with it pricing . 您想要检索定价 - 但这并不意味着您必须使用pricing来启动URL。

Promo , Offer and Customer look like resources. PromoOfferCustomer看起来像资源。 They probably have have their own URLs, like offer/123 . 他们可能有自己的URL,比如offer/123 Even if they don't have URLs they still seem to be a logical container/composition element of a Pricing . 即使他们没有URL,他们似乎仍然是Pricing的逻辑容器/组成元素。 So a Pricing should be a sub-resource of those. 所以Pricing应该是那些的子资源。

/offers/234/pricing
/promos/345/pricing
/customers/543234/pricing

If a pricing has its own id too, you can still add an additional way to list all pricings or get them by that ID: 如果定价也有自己的ID,您仍然可以添加其他方法来列出所有价格或通过该ID获取它们:

/pricings
/pricings/12

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

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