【问题标题】:Filtering parameters in Rest APIRest API 中的过滤参数
【发布时间】:2017-07-12 13:08:52
【问题描述】:

我有两个实体“人员”和“企业”,它们都有一个子资源(例如位置)

所以我有端点:

GET /persons/{id}/locations
GET /businesses/{id}/locations

现在我想要一个端点来过滤主要资源的位置。 如果我这样做:

GET /persons/{id}/locations?country={...}
GET /businesses/{id}/locations?country={...}

我将搜索特定人员/企业的位置。

过滤所有人的位置的最佳做法是什么 我有一些想法:

1. GET /persons/locations?country={...}   
2. GET /locations?entity=persons&country={...}

但不确定这些是否正常。

【问题讨论】:

    标签: rest api api-design


    【解决方案1】:

    您可以使用扩展概念来处理实体之间的关系。

    GET /persons
    
    {
      "data": [
          {"person_id": 1},
          {"person_id": 2}
     ],
     "links": [ 
        {
            "rel": "locations",
            "href": "/person/1/locations"
        },
        {
            "rel": "locations",
            "href": "/person/2/locations"
        } 
      ]
    }
    
    GET /persons?expand=locations&country={...}   
    
    {
      "data": [
          {"person_id": 1, "locations": [...]},
          {"person_id": 2, "locations": [...]}
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多