Java RestResource:无需额外请求即可获取子实体的id
我有这样的实体:Java RestResource:无需额外请求即可获取子实体的id,java,spring,rest,spring-data,spring-data-rest,Java,Spring,Rest,Spring Data,Spring Data Rest,我有这样的实体: @Entity class MyEntity { Long id; SecondEntity second; ... } @Entity class SecondEntity { Long id; ... } 我将@RestResource用于RESTAPI。 如果我请求MyEntity列表,我会得到如下结果: { "_links": { "self": {"href": "http://localhost:808
@Entity
class MyEntity {
Long id;
SecondEntity second;
...
}
@Entity
class SecondEntity {
Long id;
...
}
我将@RestResource用于RESTAPI。
如果我请求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]。秒==[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"}
}
}
您可以使用Spring Data REST的投影功能来实现这一点 这里有一个解释
这里有一个类似的问题您需要的数据(子实体的id)只能从REST请求的响应中获得。因此,您必须发送2个请求。是的。它适合我。非常感谢。