在聚合的情况下,聚合的版本必须是最后一个更新部分的版本。使用事件溯源,这很简单,因为您可以使用事件 ID 进行版本控制。
至于您的项目集合,集合资源的 etag 必须是最后一个更新的项目资源的 etag。对于每个项目,您必须在其超链接中描述 etag,或者如果所有超链接在同一资源中具有相同的版本,则可以在包含资源中描述版本。
例如,如果项目 4 是最新更新的,则 `GET /items` 应该返回类似以下内容:
{
"type": "/doc/ItemSet",
"version": "3q2teg3234",
"label": "The Collection of Expensive Items",
"size": 2,
"items": [
{
"type": "/doc/Item",
"version": "f233425wfsw",
"id": "1",
"label": "Bugatti Veyron",
"links": [
{
"label": "Read item",
"type": "/doc/Item/methods/read"
},
{
"label": "Update item",
"type": "/doc/Item/methods/update"
}
]
},
{
"type": "/doc/Item",
"version": "3q2teg3234",
"label": "Porsche 911",
"id": "4",
"links": [
{
"label": "Read item",
"type": "/doc/Item/methods/read"
},
{
"label": "Update item",
"type": "/doc/Item/methods/update"
}
]
},
],
"links": [
{
"label": "List items",
"type": "/doc/ItemSet/methods/list"
}
]
}
当然,您可以在同一文档中发送超链接描述,例如/doc/Item/methods/update
,但最好有可重用的内容,例如:
{
"type": "/doc/HyperLinkTemplate",
"method": "PUT",
"uri": "/api/items/{id}",
"headers": {
"ETag": "{version}"
},
"body": {
"label": "{label}"
},
"parameters": {
"version": {
"type": "/doc/ResourceVersion",
"value": {
"type": "doc/Query",
"query": "{context/containingResource/version}"
},
"writeable": false
},
"id": {
"type": "/doc/Item/id",
"value": {
"type": "doc/Query",
"query": "{context/containingResource/id}"
},
"writeable": false
},
"label": {
"type": "/doc/Item/label",
"value": {
"type": "doc/Query",
"query": "{context/containingResource/label}"
},
"writeable": true
}
}
}
如何使用超链接取决于您的客户端。甚至可以从中生成HTML表单,但REST主要用于M2M通信。
GET /items
将返回一个项目列表。 - stusmith