Java 使用Spring HATEOAS嵌套嵌入HAL

Java 使用Spring HATEOAS嵌套嵌入HAL,java,rest,spring-hateoas,hal,hypermedia,Java,Rest,Spring Hateoas,Hal,Hypermedia,我正在寻找如何使用Spring HATEOAS API编程嵌入HAL中的嵌套_的示例。最佳实践是什么 下面是我想要实现的一个示例: { "_links": { "self": { "href": "/invoices" } }, "_embedded": { "invoices": [ { "_links": { "self": { "href"

我正在寻找如何使用Spring HATEOAS API编程嵌入HAL中的嵌套_的示例。最佳实践是什么

下面是我想要实现的一个示例:

{
    "_links": {
        "self": { "href": "/invoices" }
    },
    "_embedded": {
        "invoices": [
            {
                "_links": {
                    "self": { "href": "/invoice/1" }
                },
                "_embedded": {
                    "items": [
                        { "_links": { "self": { "href": "/product/1" }}, "id": 1, "name": "Super cheap Macbook Pro", "price": 2.99 }
                    ]
                },
                "id": 1,
                "total": 2.99,
                "no_items": 1
            },
            {
                "_links": {
                    "self": { "href": "/invoice/2" }
                },
                "_embedded": {
                    "items": [
                        { "_links": { "self": { "href": "/product/2" }}, "id": 2, "name": "Raspberry Pi", "price": 34.87 },
                        { "_links": { "self": { "href": "/product/3" }}, "id": 3, "name": "Random product", "price": 30 },
                        { "_links": { "self": { "href": "/product/4" }}, "id": 4, "name": "More randomness", "price": 30 }
                    ]
                },
                "id": 2,
                "total": 94.87,
                "no_items": 3
            }
        ]
    }
}

有了SpringDataREST和SpringHateOAS,我看到了两种轻松实现所需的方法

  • 创建发票和项目实体,并仅为发票实体创建存储库。这将内联项目。另一方面,您无法查询项目本身,这可能不是您想要的
  • 创建这两个实体,并为它们创建存储库。现在,在Invoice存储库上创建一个链接,可以查询该链接,并嵌入项目和相应的链接集合。但也有一个缺点:嵌入的项目上不会有链接。我认为您应该能够使用投影中的资源在其中包含链接
  • 请参见使用带有项目的订单的一些示例代码:

    @Data
    @Entity
    public class Item {
      @Id
      @GeneratedValue
      private Long id;
      private String name;
    }
    
    @Data
    @Entity
    @Table(name = "customer_order")
    public class Order {
      @Id
      @GeneratedValue
      private Long id;
      private String name;
      @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
      private Collection<Item> items;
    }
    
    public interface ItemRepository extends CrudRepository<Item, Long> {
    }
    
    @RepositoryRestResource(excerptProjection = InlineItems.class)
    public interface OrderRepository extends CrudRepository<Order, Long> {
    }
    
    @Projection(name = "inlineItems", types = Order.class)
    public interface InlineItems {
      String getName();
    
      Collection<Item> getItems();
    }
    

    有了SpringDataREST和SpringHateOAS,我看到了两种轻松实现所需的方法

  • 创建发票和项目实体,并仅为发票实体创建存储库。这将内联项目。另一方面,您无法查询项目本身,这可能不是您想要的
  • 创建这两个实体,并为它们创建存储库。现在,在Invoice存储库上创建一个链接,可以查询该链接,并嵌入项目和相应的链接集合。但也有一个缺点:嵌入的项目上不会有链接。我认为您应该能够使用投影中的资源在其中包含链接
  • 请参见使用带有项目的订单的一些示例代码:

    @Data
    @Entity
    public class Item {
      @Id
      @GeneratedValue
      private Long id;
      private String name;
    }
    
    @Data
    @Entity
    @Table(name = "customer_order")
    public class Order {
      @Id
      @GeneratedValue
      private Long id;
      private String name;
      @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
      private Collection<Item> items;
    }
    
    public interface ItemRepository extends CrudRepository<Item, Long> {
    }
    
    @RepositoryRestResource(excerptProjection = InlineItems.class)
    public interface OrderRepository extends CrudRepository<Order, Long> {
    }
    
    @Projection(name = "inlineItems", types = Order.class)
    public interface InlineItems {
      String getName();
    
      Collection<Item> getItems();
    }
    

    @丹尼斯。“是的,我两个都用。”丹尼斯。是的,我两者都用。