RestResource:在没有额外请求的情况下获取subentity的id

时间:2015-03-11 17:10:06

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

我有这样的实体:

@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]。第二,我需要做两个额外的请求。这不好。

也许可以配置它提供以下资源的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"}
    }
}

1 个答案:

答案 0 :(得分:2)