我需要能够根据一组对象获取一个集合.为此,我正在考虑几种方法,但我不确定哪种方法最有意义,所以我会发布它们.如果有更好的方式我可能没有考虑过,那么请随意发表你的建议:)
例如,我将使用产品和评论(产品评论)作为资源.
目标:我们希望得到所有产品的评论
方法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
Run Code Online (Sandbox Code Playgroud)
方法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
Run Code Online (Sandbox Code Playgroud)
方法C:
GET api/Reviews { json: filters: { products: 'api/Products?filters' }...
// is this reasonable?
Run Code Online (Sandbox Code Playgroud)
在此先感谢任何帮助,如果这个问题是noob,我会道歉,我还是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?
Run Code Online (Sandbox Code Playgroud)
目标:我们希望得到所有产品的评论
基本上,GET /api/products/<id>应该返回一个product,并且GET /api/products/<id>/reviews应该返回reviews给定的全部product.
如果你想获取所有products,你将使用GET /api/products.当你考虑这两个reviews和products作为资源时,我建议你公开所有reviews:
GET /api/reviews
Run Code Online (Sandbox Code Playgroud)
但你可能想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
Run Code Online (Sandbox Code Playgroud)
此URI表示:"标识与每个关联的集合products以及每个集合.reviewsproduct
| 归档时间: |
|
| 查看次数: |
5816 次 |
| 最近记录: |