json - 分页 jsonschema/hyperschema 时保留查询参数

标签 json rest pagination jsonschema

我有一个 REST API,它有很多潜在的查询参数。

可通过类似 http://example.com/api/object?someParam=10&someOtherParam=20 的 URL 访问 API

存在大量潜在参数的地方。

响应定义如下:

{
    "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 将会爆炸,而且每次添加新的查询参数时都需要更新架构。

在我看来,这是一个非常常见的用例,但经过大量谷歌搜索后,我无法找到任何相关内容。

最佳答案

因此,我选择了一种似乎效果很好的方法。很可能有更好的解决方案,但这是我们能想出的唯一一个不会让人感觉很恶心的解决方案。

具体来说,请注意 queryString 的介绍及其在链接中的使用。

通过从传入的查询字符串中删除“page”和“perPage”字段来填充queryString,以免重复这些字段。

还值得注意的是 queryString 之前的 +,它可以防止对其进行 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"
        },
        //queryString is all of the GET parameters, in their URL form
        // e.g. "someParam=10&anotherParam=20"
        "queryString": {
            "title": "String representing the rest of the query params",
            "type": "string"
        }
    },
    "links": [
        //Added queryString to the end of the hrefs. + sign prevents URL encoding
        {
            "title": "Get object collection",
            "rel": "self",
            "method": "GET",
            "href": "/api/object?page={currPage}&perPage={perPage}&{+queryString}"
        },
        {
            "title": "Get next page",
            "rel": "next",
            "method": "GET",
            "href": "/api/object?page={nextPage}&perPage={perPage}&{+queryString}"
        },
        {
            "title": "Get prev page",
            "rel": "prev",
            "method": "GET",
            "href": "/api/object?page={prevPage}&perPage={perPage}&{+queryString}"
        }
    ]
}

希望其他人发现这很有用。

关于json - 分页 jsonschema/hyperschema 时保留查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18476601/

相关文章:

mySQL 分页使用限制检索重复行

python - 迭代嵌套的 Python 字典

javascript - 如何在 Tizen Studio 中将新信息写入 JSON 文件

java - Jackson/Gson 的良好 REST 设计

javascript - 在react中设置动态输入字段的状态

css - css 风格的 cakephp 分页结果的最佳方式是什么

json - 将 JSON POST 请求从一个 REST API 转发到另一个

javascript - 主干 : Getting JSON file from server, 使用扩展 ".json"或不使用它,带有 url 或 urlRoot

c++ - CPPRest SDK 向服务器发出 HTTP 请求

php - 分页在带有 url_suffix 的 codeigniter 2.1.0 中无法正常工作