REST集合,最好的做法是:GET api / CollectionA /< collection id> / CollectionB? [英] REST collections, good practice to do: GET api/CollectionA/<collection id>/CollectionB?
问题描述
我需要能够基于对象集合来获取一个集合。为此,我正在考虑几种方法,但不确定哪种方法最有意义,因此我将同时发布它们。如果有我可能没有考虑过的更好的方法,那么请随时发表您的建议:)
I need to be able to grab a collection based on a collection of objects. For this, I'm considering a couple of approaches, but am not sure which one makes the most sense, so I'll post them both. If there is a better way that I may not have considered, then please feel free to post your suggestion(s) :)
例如,我将使用产品和评论(即产品评论)作为资源。
For example, I'll use Products and Reviews (which are reviews of products) as resources.
目标:
我们要获得所有产品的评论
Objective: We want to get the Reviews for all of the Products
方法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
Thanks in advance for any help, and apologies if this question is noob, I'm still new to 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?
推荐答案
目标:我们想要以获得所有产品的评论
Objective: We want to get the Reviews for all of the Products
基本上, GET / api / products /< id>
应该返回产品
,并 GET / api / products /< id> /评论
应该返回给定产品
的所有条评论
。
Basically, GET /api/products/<id>
should return a product
, and GET /api/products/<id>/reviews
should return all reviews
for a given product
.
如果要提取所有产品
,则将使用 GET / api / products
。当您同时将评论
和产品
都视为资源时,建议您公开所有评论
:
If you want to fetch all products
, you will use GET /api/products
. As you consider both reviews
and products
as resources, I recommend you to expose all reviews
:
GET /api/reviews
但是您可能想要产品
和他们的评论
。这意味着将使用 / api / products
(例如一组过滤器)来获取某些产品,并使用<$ c $来扩展结果集c>评论。此扩展
术语来自OData协议: http:// www.odata.org/documentation/uri-conventions#ExpandSystemQueryOption 。
But you probably want products
with their reviews
. That would mean fetching some products using /api/products
(and a set of filters for example) and expanding the result set with reviews
. This expand
term comes from the OData protocol: http://www.odata.org/documentation/uri-conventions#ExpandSystemQueryOption.
您可以这样做:
GET /api/products?$expand=reviews
GET /api/products?filter=foo&$expand=reviews
此URI的意思是:标识个产品
的集合以及每个与每个产品
相关的评论
。
This URI means: "identifies the collection of products
as well as each of the reviews
associated with each product
.
这篇关于REST集合,最好的做法是:GET api / CollectionA /< collection id> / CollectionB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!