How to expose a complete tree structure with Sprin

2019-03-21 09:10发布

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?

3条回答
Lonely孤独者°
2楼-- · 2019-03-21 09:38

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楼-- · 2019-03-21 09:42

Try excerpts.

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

@RepositoryRestResource(excerptProjection = AllDocumentsProjection.class)
interface DocumentRepository extends CrudRepository<Document, Integer> {}
查看更多
等我变得足够好
4楼-- · 2019-03-21 10:01
@Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {

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

}

This works perfect for me.

查看更多
登录 后发表回答