【发布时间】:2014-08-12 22:44:36
【问题描述】:
我有一个简单的实体,它映射了一个集合。
@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"
}
}
仅使用 Appointment 存储库,我可以获得以下有效负载并可以发布产品列表。
{
"trackNumber" : "XYZ123",
"products" : [ {
"model" : "MODEL",
"description" : "NAME",
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/consumerappointment/appointments/1"
}
}
}
【问题讨论】:
标签: spring-data-rest