我正在开发一个Java Spring引导应用程序,使用React JS作为前端。在我的react js项目中,在表单提交时,我将数据发送到Spring API。由于某些原因,我无法将javascript JSON数组属性映射到java数组属性。除数组外,所有其他字符串数据类型属性都匹配。这是我的例子,在react js前端。
export function addBooking(bookings) {
return new Promise((resolve, reject) => {
axios.post(url, bookings)
.then((response) => {
resolve(response.data);
})
.catch((error) => {
reject(error);
});
});
}
上面是我的react代码,它发送这个JSON对象,下面是。
{“街道”:"someStreet",“城市”:"somecity","contactNumber“:"0000",”piecesData“:{”id“:”111“,”weight“:”22“,”length“:”32“,”width“:”23“,”height“:”23“”type“:”heavyLoad“},{“id”:“111”,“weight”:“22”,“length”:“32”,“width”:“23”,“height”:“23”“type”:“heavyLoad”}}
由于某些原因,在Spring服务器端,唯一映射的属性是街道、城市和contactNumber。但是,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将预订及其pieceDatas数组保存到数据库中。
下面是我的java PieceData对象:
@Entity
@Table(name=" PieceData ")公共类PieceData实现了可序列化{
@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;
}
}
发布于 2018-09-14 06:25:29
我通过向List pieceData类型添加json属性注释来解决我的问题:
@JsonProperty(value = "piecesData")
private List<PieceData> piecesDatas;
https://stackoverflow.com/questions/52310434
复制相似问题