我有一个简单的实体,映射了一个集合.
@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;
}
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我只包含一个PagingAndSortingRepository for Appointment.我可以使用以下有效负载调用POST命令.
{
"trackNumber" : "XYZ123",
"products": [
{"model" : "MODEL",
"description" : "NAME"
}]
}
Run Code Online (Sandbox Code Playgroud)
当我为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"
}
}
Run Code Online (Sandbox Code Playgroud)
只有Appointment存储库,我得到以下有效负载,并可以发布产品列表.
{
"trackNumber" : "XYZ123",
"products" : [ {
"model" : "MODEL",
"description" : "NAME",
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/1"
}
}
}
Run Code Online (Sandbox Code Playgroud)
让我们退后一步,确保您了解这里发生的情况:如果检测到存储库,Spring Data REST 会公开一组专用资源,用于管理存储库通过 HTTP 处理的聚合。因此,如果您有多个彼此相关的实体的存储库,则该关系将表示为链接。这就是为什么AppointmentRepository在products创建ProductRepository.
如果要将两个存储库公开为资源,则需要Product在有效负载中传递实例的 URI,以便POST创建Appointment. 这意味着,不要发布此内容:
{ "trackNumber" : "XYZ123",\n "products": [\n { "model" : "MODEL",\n "description" : "NAME"\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n\n你会创建Product第一个:
POST /products\n{ "model" : "MODEL",\n "description" : "NAME" }\n\n201 Created\nLocation: \xe2\x80\xa6/products/4711\nRun Code Online (Sandbox Code Playgroud)\n\n然后将产品的ID交给payload Appointment:
{ "trackNumber" : "XYZ123",\n "products": [ "\xe2\x80\xa6/products/4711" ]}\nRun Code Online (Sandbox Code Playgroud)\n\n如果您不想要任何这些(Product首先不暴露任何资源,请使用@RepositoryRestResource(exported = false)on PersonRepository。这仍然会为您留下为存储库创建的 bean 实例,但不会导出任何资源,并且为Appointments 暴露的资源返回到内联 相关Product.
| 归档时间: |
|
| 查看次数: |
1332 次 |
| 最近记录: |