【问题标题】:RestResource: get id of subentity without extra requestRestResource:无需额外请求即可获取子实体的 ID
【发布时间】:2015-03-11 17:10:05
【问题描述】:

我有这样的实体:

@Entity
class MyEntity {
    Long id;
    SecondEntity second;
    ...
}

@Entity
class SecondEntity {
    Long id;
    ...
}

我将@RestResource 用于rest-API。 如果我请求 MyEntity 列表,我会得到如下结果:

{
    "_links": {
      "self": {"href": "http://localhost:8080/api/MyEntity/1"},
      "second": {"href": "http://localhost:8080/api/MyEntity/1/second"}
    }
},
{
    "_links": {
      "self": {"href": "http://localhost:8080/api/MyEntity/2"},
      "second": {"href": "http://localhost:8080/api/MyEntity/2/second"}
    }
}

如果我想检查,是 [0].second == [1].second,我需要做两个额外的请求。这不好。

也许可以配置它提供以下资源的 RestResource?

{
    "_links": {
      "self": {"href": "http://localhost:8080/api/MyEntity/1"},
      "second": {"href": "http://localhost:8080/api/SecondEntity/12"}
    }
},
{
    "_links": {
      "self": {"href": "http://localhost:8080/api/MyEntity/2"},
      "second": {"href": "http://localhost:8080/api/SecondEntity/45"}
    }
}

【问题讨论】:

  • 您需要的数据(子实体的id)只能从REST请求的响应中获取。所以你必须发送 2 个请求。

标签: java spring rest spring-data spring-data-rest


【解决方案1】:

【讨论】:

  • 是的。它适合我。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2013-06-02
相关资源
最近更新 更多