How to expose a complete tree structure with Spring Data REST and HATEOAS?

坚强是说给别人听的谎言 提交于 2019-12-21 02:43:14

问题


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)

}

and a projection

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

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

}

When I make a GET request with url

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?


回答1:


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

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

}

This works perfect for me.




回答2:


I'm almost certain there is no way to recursively embed resources via projections. Only other thing I think of is to handle this logic manually in the controller :/




回答3:


Try excerpts.

You should add to your repository definition the excerptProjection field like below:

@RepositoryRestResource(excerptProjection = AllDocumentsProjection.class)
interface DocumentRepository extends CrudRepository<Document, Integer> {}


来源:https://stackoverflow.com/questions/36542881/how-to-expose-a-complete-tree-structure-with-spring-data-rest-and-hateoas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!