【发布时间】:2016-03-17 19:08:19
【问题描述】:
如何使用 HATEOAS 从对象的集合中删除资源?
PUT 将设置集合。 PATCH 将允许部分更新/添加。
但是如何进行部分更新/删除?
我真的需要 POST 整个 uri-list 减 1 才能删除单个项目吗?
以这个对象为例:
{
"name": "Bob Test",
"description": "this is the descript",
"_links": {
"self": {
"href": "http://localhost/example/1"
},
"example": {
"href": "http://localhost/example/1"
},
"citations": {
"href": "http://localhost/example/1/citations"
},
}
}
该对象有很多引用(集合):
{
"_embedded": {
"citations": [
{
"content": "asdfasdf",
"anchor": null,
"_links": {
"self": {
"href": "http://localhost/citations/1"
},
"citation": {
"href": "http://localhost/citations/1"
},
"bioMarker": {
"href": "http://localhost/citations/1/example"
}
}
},
{
"content": "c2",
"anchor": "prf",
"_links": {
"self": {
"href": "http://localhost/citations/2"
},
"citation": {
"href": "http://localhost/citations/2"
},
"bioMarker": {
"href": "http://localhost/citations/2/example"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost/example/1/citations"
}
}
}
现在假设我要删除 http://localhost/citations/2,如何从 http://localhost/example/1/citations 集合中删除此特定项目?
【问题讨论】:
-
您能否添加一些语法代码以及预期的输入和输出?我并不完全清楚你想要完成什么。
-
@woemler 当然,但在方向之外,我不确定 hal-json 将如何提供帮助。
-
“PATCH 将允许部分更新/添加。” 是吗?如果是这样,从集合中删除一个项目不是对集合的部分更新吗? “我真的需要 POST 整个 uri-list 减 1” 你不是说 PUT 吗? POST 将添加一个项目。我假设您使用 Spring Data REST。如果是这样,那么这是一个重要的事实。 HATEOAS 不会确定如何从集合中删除项目,但您的服务的实现会。
-
@zeroflagL 用于实现 API 的代码是一些存储库和域。很苗条。您能否提供有关实施应如何删除项目的指导?我读过的几乎所有资源都在谈论读取数据,而很少谈论更新集合。
-
我需要更多关于资源之间关系的信息。看起来是双向的关系。目前尚不清楚一种资源是否可以在没有另一种的情况下存在。如果没有示例就不能存在引用,我建议将示例视为聚合。我想知道为什么资源有一个与
self链接具有相同值的链接。实体是否指向自己?您可以提供一个http://localhost/example/1/citations/{id of citation}链接,可以使用DELETE调用。
标签: spring spring-data-rest spring-hateoas