【问题标题】:How to expose a complete tree structure with Spring Data REST and HATEOAS?如何使用 Spring Data REST 和 HATEOAS 公开完整的树结构?
【发布时间】:2016-08-01 06:31:41
【问题描述】:

我有一个 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 请求时

localhost:8080/documents/1?projection=all

我只得到根文档的第一个孩子。不是孩子的孩子。这可能与预测?还是有别的办法?

【问题讨论】:

    标签: jpa tree projection spring-data-rest hateoas


    【解决方案1】:
    @Projection(name = "all", types = Document.class)
    public interface AllDocumentsProjection {
    
        int getId();
        String getText();
        Set<AllDocumentsProjection> getChildren();
    
    }
    

    这对我来说很完美。

    【讨论】:

      【解决方案2】:

      我几乎可以肯定没有办法通过projections 递归嵌入资源。我唯一想到的另一件事是在控制器中手动处理这个逻辑:/

      【讨论】:

        【解决方案3】:

        试试excerpts

        您应该将excerptProjection 字段添加到您的存储库定义中,如下所示:

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

        【讨论】:

          猜你喜欢
          • 2014-03-07
          • 2018-07-23
          • 2016-04-30
          • 2013-10-31
          • 2014-08-16
          • 2018-02-23
          • 2016-04-02
          • 2014-07-05
          • 2016-05-23
          相关资源
          最近更新 更多