【发布时间】:2019-02-17 23:53:41
【问题描述】:
我正在开发一个以 React JS 作为前端的 Java Spring 启动应用程序。在我的 react js 项目中,在提交表单时,我将数据发送到 Spring API。出于某种原因,我无法将 javascript JSON 数组属性映射到 java 数组属性。除数组外,所有其他 String 数据类型属性都匹配。这是我的例子,在 react js 前端。
export function addBooking(bookings) {
return new Promise((resolve, reject) => {
axios.post(url, bookings)
.then((response) => {
resolve(response.data);
})
.catch((error) => {
reject(error);
});
});
}
上面是我的反应代码,它在下面发送这个 JSON 对象。
{ “街道”:“一些街道”, “城市”:“某个城市”, “联系人号码”:“0000”, “片断数据”:[ { “id”:“111”, “重量”:“22”, “长度”:“32”, “宽度”:“23”, “身高”:“23” “类型”:“重载” }, { “id”:“111”, “重量”:“22”, “长度”:“32”, “宽度”:“23”, “身高”:“23” “类型”:“重载” } ] }
出于某种原因,在 Spring 服务器端,唯一被映射的属性是街道、城市和联系人号码。但是,piecesData 没有映射到它对应的 java 数组属性。
这是 Java 模型对象:
公共类测试实现可序列化{
public String city;
public String street;
public String contactNumber;
@OneToMany(
cascade = {CascadeType.ALL},
fetch = FetchType.EAGER
)
@JoinColumn(name = "booking_id", referencedColumnName = "booking_id")
public PieceData[] pieceData;
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public PieceData[] getPieceData() {
return pieceData;
}
public void setPieceData(PieceData[] pieceData) {
this.pieceData = pieceData;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
contactNumber = contactNumber;
}
}
一旦我能够获得所有这些数据,那么我希望能够使用 JPA 将 Booking 和它的 pieceDatas 数组保存到数据库中。
下面是我的 java PieceData 对象:
@Entity
@Table(name="pieceData") 公共类 PieceData 实现 Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String weight;
private String length;
private String width;
private Integer height;
private Integer booking_id;
public Integer getBooking_id() {
return this.booking_id;
}
public void setBooking_id(Integer booking_id) {
this.booking_id = booking_id;
}
public PieceData() {
}
public PieceData(Integer height, String length, String width, String weight) {
this.length = length;
this.width = width;
this.weight = weight;
this.height = height;
}
// Weight
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
// Length
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
// Width
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
// Height
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
}
【问题讨论】:
-
"piecesData 没有映射到它对应的 java 数组属性。"这里你说的正确是什么意思?数据部分映射?
-
@Abinash Ghosh 它根本没有被映射
标签: json spring spring-boot jpa