axios发布请求未映射到Requestbody中的Java对象属性

时间:2018-09-13 09:14:01

标签: json spring spring-boot jpa

我正在使用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);
            });
    });
}

上面是我的React代码,它在下面发送此JSON对象。

  

{    “ street”:“ someStreet”,    “ city”:“ somecity”,    “ contactNumber”:“ 0000”,    “ piecesData”:[       {       “ id”:“ 111”,       “重量”:“ 22”,       “长度”:“ 32”,       “宽度”:“ 23”,       “身高”:“ 23”       “ type”:“ heavyLoad”       },       {       “ id”:“ 111”,       “重量”:“ 22”,       “长度”:“ 32”,       “宽度”:“ 23”,       “身高”:“ 23”       “ type”:“ heavyLoad”       }    ]   }

由于某种原因,在Spring服务器端,唯一被映射的属性是street,city和contactNumber。但是,piecesData不会映射到其对应的java数组属性。

这是Java模型对象:

  

公共类Test实现了Serializable {

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;
}

}

0 个答案:

没有答案