我有这样的实体:
@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"}
}
}
答案 0 :(得分:2)
您可以使用Spring Data REST的投影功能
来完成此操作 的解释以下是类似的问题How to return representations of associations in a Spring Data REST resource?