我一直在想,而不是像这样更传统的布局:
api/Products
GET // gets product(s) by id
PUT // updates product(s) by id
DELETE // deletes (product(s) by id
POST // creates product(s)
具有单数和复数会更有用,例如:
api/Product
GET // gets a product by id
PUT // updates a product by id
DELETE // deletes a product by id
POST // creates a product
api/Products
GET // gets a collection of products by id
PUT // updates a collection of products by id
DELETE // deletes a collection of products (not the products themselves)
POST // creates a collection of products based on filter parameters passed
因此,要创建产品集合,您可以执行以下操作:
POST api/Products {data: filters} // returns api/Products/<id>
然后,要引用它,您可以这样做:
GET api/Products/<id> // returns array of products
在我看来,以这种方式做事的主要优点是可以轻松地缓存产品集合。例如,一个人可能花一小时的时间来收集产品,从而大大减少了服务器上的呼叫。当然,我目前只看到以这种方式做事的好处,这有什么坏处?