【发布时间】: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