Jpa 如何使用Spring数据REST和HATEOAS公开完整的树结构?
我有一个JPA树结构Jpa 如何使用Spring数据REST和HATEOAS公开完整的树结构?,jpa,tree,projection,spring-data-rest,hateoas,Jpa,Tree,Projection,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", fe
@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)
}
@实体
公共类文档{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
私有字符串文本;
@许多酮
@JoinColumn(name=“parent”)
文件母公司;
@OneToMany(mappedBy=“parent”,fetch=FetchType.EAGER)
设置儿童;
(接球手和接球手)
}
还有一个投影
@Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {
int getId();
String getText();
Set<Document> getChildren();
}
@Projection(name=“all”,types=Document.class)
公共接口AllDocumentsProject{
int getId();
字符串getText();
设置getChildren();
}
当我使用url发出GET请求时
localhost:8080/documents/1?projection=all
我只获得根文档的第一个子级。不是孩子的孩子。这在预测中是可能的吗?或者还有其他方法吗?我几乎可以肯定,没有方法通过递归方式嵌入资源。我想到的另一件事是在控制器中手动处理此逻辑://p>试试 您应该向存储库定义中添加
摘录投影
字段,如下所示:
@RepositoryRestResource(excerptProjection = AllDocumentsProjection.class)
interface DocumentRepository extends CrudRepository<Document, Integer> {}
@RepositoryRestResource(摘录projection=AllDocumentsProjection.class)
接口DocumentRepository扩展了Crudepository{}
@Projection(name=“all”,types=Document.class)
公共接口AllDocumentsProject{
int getId();
字符串getText();
设置getChildren();
}
这对我来说非常合适
@Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {
int getId();
String getText();
Set<AllDocumentsProjection> getChildren();
}