我有一个 REST API,它有很多潜在的查询参数。
API 是通过类似http://example.com/api/object?someParam=10&someOtherParam=20的 URL 访问的
有大量潜在参数的地方。
响应定义如下:
{
"title": "Object Collection",
"type": "object",
"properties": {
"collection": {
"title": "Collection",
"type": "array",
"items": {
"$ref": "/schema/object.json"
}
},
"currPage": {
"title": "Current Page",
"type": "int"
},
"nextPage": {
"title": "Next Page",
"type": "int"
},
"prevPage": {
"title": "Previous Page",
"type": "int"
},
"perPage": {
"title": "Per Page",
"type": "int"
},
"totalCount": {
"title": "Total Count",
"type": "integer"
}
},
"links": [
{
"title": "Get object collection",
"rel": "self",
"method": "GET",
"href": "/api/object?page={currPage}&perPage={perPage}"
},
{
"title": "Get next page",
"rel": "next",
"method": "GET",
"href": "/api/object?page={nextPage}&perPage={perPage}"
},
{
"title": "Get prev page",
"rel": "prev",
"method": "GET",
"href": "/api/object?page={prevPage}&perPage={perPage}"
}
]
}
当然,当前定义的问题在于它在尝试通过链接转到另一个页面时会抛出查询参数。
有什么好方法可以解释任意数量的参数吗?
从理论上讲,我可以将所有可能性添加到我的回复中,例如
"properties": {
...
"someParam" : {
"description": "Some Param"
},
"someOtherParam" : {
"description": "Another param"
}
}
并使我的链接看起来像:
{
"title": "Get prev page",
"rel": "prev",
"method": "GET",
"href": "/api/object?page={prevPage}&perPage={perPage}&someParam={someParam}&someOtherParam={someOtherParam}"
}
但这很快就变得很麻烦,特别是考虑到大量的查询参数。
URL 将爆炸,而且每次添加新查询参数时都需要更新架构。
这让我觉得这是一个非常常见的用例,但经过大量的谷歌搜索后,我无法在上面找到很多东西。