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

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

问题描述

我有一个JPA树结构

@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天全站免登陆