如何在春季休养期反应中纳入参考?

时间:2018-10-04 19:48:30

标签: java spring spring-data-jpa spring-data

我用spring定义了两个JPARepository:“ Person”和“ Address”。我还指定了“人”和“地址”之间的关系。

我可以通过以下方式吸引所有人: http://localhost:8080/person/

以及所有地址为:http://localhost:8080/address/

我也可以通过http://localhost:8080/person/1/address来获得一个人的地址

现在,我想在收到请求时将每个人的地址作为嵌套地址获取:http://localhost:8080/person/

如何在回复中包含这些关系?

@nicolasl要求的我的课程

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

    @OneToOne
    @JoinColumn(name = "address_id")
    private Address address;


    //Getter/Setter here


}

@Entity
public class Address {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String location;

    @OneToOne(mappedBy = "address")
    private Person person;

    //Getter/Setter here
}

1 个答案:

答案 0 :(得分:0)

好吧,我想您就是不能,没有使用spring提供的自动休息引擎。其余的API将返回链接以检索JSON响应的“ _links”部分中的相关实体(您可以在http://stateless.co/hal_specification.html中了解有关此实体的更多信息)。您必须调用这些链接才能检索您的地址。

解决此问题的另一种方法是实现自己的存储库并组装自己的JSON响应。