Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RestResource:无需额外请求即可获取子实体的id_Java_Spring_Rest_Spring Data_Spring Data Rest - Fatal编程技术网

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个请求。是的。它适合我。非常感谢。