分页jsonschema/hyperschema时保留查询参数
我有一个RESTAPI,它有很多潜在的查询参数 API是通过URL访问的,如 其中存在大量潜在参数 响应定义如下:分页jsonschema/hyperschema时保留查询参数,json,rest,pagination,jsonschema,Json,Rest,Pagination,Jsonschema,我有一个RESTAPI,它有很多潜在的查询参数 API是通过URL访问的,如 其中存在大量潜在参数 响应定义如下: { "title": "Object Collection", "type": "object", "properties": { "collection": { "title": "Collection", "type": "array", "items": {
{
"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的介绍及其在链接中的使用 queryString是通过从传入的查询字符串中去掉“page”和“perPage”字段来填充的,这样就不会重复这些字段 还值得注意的是,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}"
}
]
}
希望其他人觉得这很有用。只是想让它保持不变:) 据 如果客户机遵循第一个链接,URI可能会扩展到 “/15/评论”。对于第二个链接,方法是“GET”(默认值) 对于HTTP),因此遵循此链接的客户端将向 用于生成以下内容的URL: “/15/comments?searchTerm=JSON&itemsPerPage=50”。第三个链接定义了 一种可能的交互,其中客户端将发布到URI(例如 “/15/comments”),其中post数据是 新评论 所以您应该在链接的schema属性中描述查询参数 因此,您可以为查询对象创建一个单独的模式,您可以在链接中引用该模式,而这不需要通用模式中的其他属性。谢谢:)我将在我自己的API中窃取您的想法:)谢谢!
{
"title": "News post",
...
"links": [
{
"rel": "comments",
"href": "/{id}/comments"
},
{
"rel": "search",
"href": "/{id}/comments",
"schema": {
"type": "object",
"properties": {
"searchTerm": {
"type": "string"
},
"itemsPerPage": {
"type": "integer",
"minimum": 10,
"multipleOf": 10,
"default": 20
}
},
"required": ["searchTerm"]
}
},
{
"title": "Post a comment",
"rel": "create",
"href": "/{id}/comments",
"method": "POST",
"schema": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
}
]
}