问题中提到的方法实际上意味着对于ids
的每个组合都有一个资源。比方说,我们有2 ids
:1和2
/items/1,2
/items/2,1
上述代表不同的资源,虽然结果是一样的。 这可能会让API的使用者感到困惑。
模型化的另一种方法是通过查询参数作为过滤语义。 我们假设,id
实际上是资源的一个字段。
例如,获取item
通过id
1:
GET
/items/1
Response:
{
"id": 1,
"type": "table",
"color": "black",
...
}
所以现在的问题是,如果我有什么需要得到几个项目作为散装? 您可以将此问题概括为根据某些字段上的值过滤items
的常见问题。 例如: - 让所有类型表
GET
/items?query="name='table'"
Response:
{
"data": [
{
"id": 1,
"type": "table",
"color": "black",
...
},
{
"id": 2,
"type": "table",
"color": "grey",
...
},
{
"id": 6,
"type": "table",
"color": "brown",
...
}
]
}
的items
所以同样的问题可以问得到items
其中id
是1
或2
。 比方说,我们在query
GET
/items?query="id=1||id=2"
Response:
{
"data": [
{
"id": 1,
"type": "table",
"color": "black",
...
},
{
"id": 2,
"type": "table",
"color": "grey",
...
}
]
}
语法什么的RESTful约束你关心的或操作
||
模型? URI只是遵循RFC 3986中规定的规则的标识符。就我个人而言,使用逗号分隔值列表作为URI标识符的一部分看起来没有问题。 –