0

There are products and reviews of products. So, I can get product list like this...

GET /products

also I can fetch one product or reviews or the product like this...

GET /products/{productID}
GET /products/{productID}/reviews

Those are clear. but the problem happens when I want to get all reviews I wrote. I could create uri like this..

1. GET /products/reviews?author=myId

or

2. GET /reviews?author=myID

However, the problem of first one comes from conflict between {productID} and reviews. the problem of next one comes from relation products and reviews because reviews should be under the products according to the hierarchy.

How can I get all reviews I wrote with RESTful API?

4

3 回答 3

0

要获得给定作者为所有产品撰写的所有评论,一种 RESTful 方式可以做到这一点

获取 /authors/{authorId}/reviews

如果您想获取给定作者为单个产品撰写的所有评论,那么您可以使用

(a) 获取 /authors/{authorId}/products/{productId}/reviews

我喜欢这种方法,而不是在查询字符串中指定 productId 或 authorId。作者包含产品,产品包含评论,所以这是一个不错的 RESTful URL。但是,如果您想使用查询字符串,则可以使用以下任一方法:

(b) 获取 /products/{productId}/reviews?authorId={authorId}

(c) 获取 /authors/{authorId}/reviews?productId={productId}

但我个人更喜欢选项(a)而不是选项(b)或(c)。我认为它以更简洁的方式描述了对象层次结构。

于 2012-06-26T02:16:15.283 回答
0

通常路线是这样的

获取 /products/:product_id/reviews?author=myID

所以参数是 - product_id - author

这样,您将获得某个产品和作者的所有评论。

但是,如果您想要所有评论,那么这是正确的:

获取 /reviews?author=myID

于 2012-06-22T08:45:57.150 回答
0

如果我是你,我会使用第二个,因为平面 URI 通常比分层 URI 短,而且较短的 URI 更容易路由:意外输入 URI 模板更难。

所以使用

  • GET /reviews?author={myID}
  • GET /reviews?product={productID}

也可以。(如果您有不同的评论类型,也许最好使用product-reviews而不是仅仅使用reviews。)

只是为了避免误解 REST 没有 URI 结构约束,这些是实现细节,因此您可以使用两个建议的 URI。您应该记录它们并应用 HATEOAS 约束。

于 2015-11-30T13:02:25.957 回答