[英]Filtering parameters in Rest API
I have two entities 'persons' and 'businesses' and both of them have a sub-resource (eg locations) 我有两个实体“个人”和“业务”,并且它们都有一个子资源(例如位置)
So I have endpoints: 所以我有端点:
GET /persons/{id}/locations
GET /businesses/{id}/locations
Now I want an endpoint to filter locations of a main resource. 现在,我希望端点过滤主资源的位置。 If I do: 如果我做:
GET /persons/{id}/locations?country={...}
GET /businesses/{id}/locations?country={...}
I will search locations of a specific person/business. 我将搜索特定人员/公司的位置。
What is the best practice to filter locations of all persons I have some ideas: 什么是过滤所有人的最佳实践,我有一些想法:
1. GET /persons/locations?country={...}
2. GET /locations?entity=persons&country={...}
But not sure these are fine. 但不确定这些是否可以。
You can work with the expand concept where you can be used with the relationship between the entities. 您可以使用扩展概念,在其中您可以使用实体之间的关系。
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": [...]}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.