我有一个简单的实体,其中映射了一个集合。
@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 for Appointment。我可以使用以下有效负载调用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"
}
}
只有Appointment存储库我得到以下有效负载,并且可以发布产品列表。
{
"trackNumber" : "XYZ123",
"products" : [ {
"model" : "MODEL",
"description" : "NAME",
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/1"
}
}
}
答案 0 :(得分:4)
让我们退后一步,确保您了解这里发生了什么:如果检测到存储库,Spring Data REST会为其公开dedicated set of resources来管理存储库通过HTTP处理的聚合。因此,如果您拥有彼此相关的多个实体的存储库,则该关系将表示为链接。这就是为什么您看到内联的AppointmentRepository
内联产品以及创建products
后ProductRepository
链接的原因。
如果要将两个存储库作为资源公开,则需要在Product
的有效负载中提交POST
个实例的URI,以创建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
。这仍然会让您留下bean实例为repo创建,但没有导出资源,并且Appointment
的资源暴露回内联相关的Product
。