spring-data-rest - 无法发布集合

标签 spring-data-rest

我有一个简单的实体,其中映射了一个集合。

@Entity
public class Appointment Identifiable<Integer>   {  

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonIgnore
    private Integer id;

    @Column(name="TRAK_NBR")
    private String trackNumber;

    @OneToMany(fetch =FetchType.EAGER, cascade= CascadeType.ALL)
    @JoinColumn(name="CNSM_APT_VER_WRK_I", nullable = false)
    private Set<Product> products = new HashSet<Product>();
}

@Entity
public class Product implements Identifiable<Integer> {

    @Id
    @Column(name = "CNSM_PRD_VER_WRK_I")
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonIgnore
    private Integer id;

    @Column(name = "PRD_MDL_NBR")
    private String model;

    @Column(name = "PRD_SPEC_DSC")
    private String description;
}

在我的应用程序中,当我只包含一个用于约会的 PagingAndSortingRepository 时。我可以使用以下有效负载调用 POST 命令。
{
  "trackNumber" : "XYZ123",
  "products": [
    {"model" : "MODEL",
     "description" : "NAME"
    }]
}

当我为 Product 添加 PagingAndSortingRepository 并尝试相同的 POST 时,我收到以下错误消息。
{
  "cause" : {
    "cause" : {
      "cause" : null,
      "message" : null
    },
    "message" : "(was java.lang.NullPointerException) (through reference chain: com..model.Appointment[\"products\"])"
  },
  "message" : "Could not read JSON: (was java.lang.NullPointerException) (through reference chain: com.model.Appointment[\"products\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.model.AppointmentVerification[\"products\"])"
}

My GET payload with both Repositories returns this.  This is my desired format.  The link to products should be included

{
  "trackNumber" : "XYZ123", 
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/consumerappointment/appointments/70"
    },
    "products" : {
      "href" : "http://localhost:8080/consumerappointment/appointments/70/products"
  }
}

仅使用约会存储库,我就可以获得以下有效负载,并且可以发布产品列表。
{
  "trackNumber" : "XYZ123",
  "products" : [ {
    "model" : "MODEL",
    "description" : "NAME",
  } ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/consumerappointment/appointments/1"
    }
  }
}

最佳答案

让我们退后一步,确保您了解这里发生的事情:如果检测到存储库,Spring Data REST 会公开 dedicated set of resources以便它通过 HTTP 管理存储库处理的聚合。因此,如果您有多个相互关联的实体的存储库,则该关系表示为链接。这就是为什么您看到产品内嵌只有 AppointmentRepository到位和products创建 ProductRepository 后链接到位.

如果要将两个存储库公开为资源,则需要传递 Product 的 URI。 POST 的有效负载中的实例创建一个 Appointment .这意味着,而不是发布这个:

{ "trackNumber" : "XYZ123",
  "products": [
    { "model" : "MODEL",
      "description" : "NAME"
    }
  ]
}

你会创建一个 Product第一的:
POST /products
{ "model" : "MODEL",
  "description" : "NAME" }

201 Created
Location: …/products/4711

然后将产品的ID交给Appointment有效载荷:
{ "trackNumber" : "XYZ123",
  "products": [ "…/products/4711" ]}

如果您不想要任何这些(首先没有为 Product 公开资源,请在 @RepositoryRestResource(exported = false) 上使用 PersonRepository 。这仍然会给您留下为 repo 创建的 bean 实例,但没有导出资源和为 Appointment s 公开的资源回到内联相关的 Product s。

关于spring-data-rest - 无法发布集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24356680/

相关文章:

java - 数据库更新后运行的 PUT 和 PATCH 的 Spring 数据剩余验证

java - Spring - 如何在单个资源上应用投影?

java - 如何自定义 Spring Data REST 以对存储库资源使用多段路径?

java - 在 spring-data-jpa 查询中使用 findByOrganizerId

spring - 如何使用 Spring data REST 在 pagingAndSorting 存储库接口(interface)中反转 findAll() 查询

java - Spring 数据休息2.6.10 : Find by embedded attributes value via Rest api

spring-data-rest - Spring 数据 REST : Update a resource´s association using proper HTTP method

spring - 更新对多关联

Spring HATEOAS/MockMvc/JsonPath 最佳实践

java - Spring Data Rest - 具有默认值的参数