【问题标题】:How Do I Remove a Linked Resource from a Collection in HATEOAS?如何从 HATEOAS 中的集合中删除链接资源?
【发布时间】: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


【解决方案1】:

从响应中获取内容类型并将其发送到请求标头“Accept”中。通常是“application/xxxxx+json”

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多