REST collections, good practice to do: GET api/CollectionA//CollectionB?

前端 未结 1 1604
野趣味
野趣味 2021-01-05 19:08

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, s

相关标签:
1条回答
  • 2021-01-05 19:39

    Objective: We want to get the Reviews for all of the Products

    Basically, GET /api/products/<id> should return a product, and GET /api/products/<id>/reviews should return all reviews for a given product.

    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
    

    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.

    You could do:

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

    This URI means: "identifies the collection of products as well as each of the reviews associated with each product.

    0 讨论(0)
提交回复
热议问题