【问题标题】:How do you add multiple links of the same type to a resource using Spring HATEOAS?如何使用 Spring HATEOAS 将多个相同类型的链接添加到资源中?
【发布时间】:2019-01-06 03:19:52
【问题描述】:

HAL spec that I am working with 表示您可以在这样的资源上拥有多个相同类型的链接:

{
    "_links": {
      "items": [{
          "href": "/first_item"
      },{
          "href": "/second_item"
      }]
    }
}

但是 Spring ResourceSupport 类型似乎只有一个 add() 方法来添加单个链接。所以我可以写

order.add(linkTo(methodOn(OrderController.class).order(id)).withRel("item"));

添加一个这样的,但我看不到如何根据规范获取一个引用为 items 的数组。

【问题讨论】:

  • 你能调用两次吗?
  • 我会被诅咒的!您应该将其添加为答案,我很乐意接受!

标签: hateoas spring-hateoas


【解决方案1】:

如果您使用相同的 withRel 值调用 ResourceSupport::add() 两次(或更多次),它会创建一个包含每个项目的名称的数组 ref。

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    相关资源
    最近更新 更多