如何使用 Spring Data REST 和 HATEOAS 公开完整的树结构? [英] How to expose a complete tree structure with Spring Data REST and HATEOAS?

查看:18
本文介绍了如何使用 Spring Data REST 和 HATEOAS 公开完整的树结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JPA 树结构

I have a JPA tree structure

@Entity
public class Document {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;
   private String text;

   @ManyToOne
   @JoinColumn(name = "parent")
   Document parent;

   @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
   Set<Document> children;

   (getters and setters)

}

和投影

@Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {

    int getId();
    String getText();
    Set<Document> getChildren();

}

当我使用 url 发出 GET 请求时

When I make a GET request with url

localhost:8080/documents/1?projection=all

localhost:8080/documents/1?projection=all

我只得到根文档的第一个孩子.不是孩子的孩子.这可以通过投影实现吗?或者有其他方法吗?

I only get the first children of the root document. Not children of the children. Is this possible with projections? Or is there an other way?

推荐答案

@Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {

    int getId();
    String getText();
    Set<AllDocumentsProjection> getChildren();

}

这对我来说很完美.

这篇关于如何使用 Spring Data REST 和 HATEOAS 公开完整的树结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆