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

3

5

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?

Transplant answered 11/4, 2016 at 7:54 Comment(0)
V
4
@Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {

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

}

This works perfect for me.

Vodka answered 28/1, 2018 at 21:33 Comment(0)
S
1

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 :/

Swiss answered 11/4, 2016 at 14:57 Comment(0)
P
-1

Try excerpts.

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

@RepositoryRestResource(excerptProjection = AllDocumentsProjection.class)
interface DocumentRepository extends CrudRepository<Document, Integer> {}
Plank answered 11/4, 2016 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.