REST收藏,好的做法要做到:获取API / CollectionA / / Collecti

2019-06-27 11:13发布

我需要能够根据对象的集合抢集合。 对于这一点,我考虑了几个办法,但我不知道哪一个是最有意义的,所以我会后他们俩。 如果有更好的办法,我还没有考虑,那么请随意张贴您的建议(S):)

例如,我将使用产品和评论(它们的产品评论)的资源。

目标:我们希望得到所有产品的评测

解决方法A:

POST api/Products { json: filters, limits, etc } // returns api/Products/<id>
GET api/Products/<id>/Reviews { json: filters, limits, etc } // returns json array 
// ... of reviews present in products
// id in this case would refer to the collection, which would be cached

方法B:

GET api/Reviews { json: filters } // returns json array of reviews, but needs to 
// ... have either all of the product ids passed as an array (not practical with 
// ... huge collections), or needs to have all of the api/Products filters passed, 
// ... in which case making the code unDRY

方法C:

GET api/Reviews { json: filters: { products: 'api/Products?filters' }...
// is this reasonable?

在此先感谢您的帮助,并道歉,如果这个问题是小白,我还是新的REST

方法d:

access individual products
GET api/Product/<id>

access collection of products:
POST api/Products?filters=stuff..
GET api/Products/<id>

access collection of products' reviews

POST api/Products/<id>/Reviews?filters=stuff
GET api/Products/<id>/Reviews/<id>

... this would thus allow us to cache the result easily, does this seem reasonable to you? 

Answer 1:

目标:我们希望得到所有产品的评测

基本上, GET /api/products/<id>应该返回一个product ,和GET /api/products/<id>/reviews应返回所有reviews对于给定的product

如果你想获取所有products ,你会使用GET /api/products 。 当你同时考虑reviewsproducts的资源,我建议你揭露所有reviews

GET /api/reviews

但是,你可能想products 他们的reviews 。 这将意味着使用抓取了一些产品/api/products (以及一组过滤器为例)和扩大的结果设置reviews 。 这expand术语来自OData协议: http://www.odata.org/documentation/uri-conventions#ExpandSystemQueryOption 。

你可以这样做:

GET /api/products?$expand=reviews

GET /api/products?filter=foo&$expand=reviews

这意味着URI:“标识的收藏products以及每个reviews与每个相关的product



文章来源: REST collections, good practice to do: GET api/CollectionA//CollectionB?