首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >axios post请求未映射到Requestbody中的java对象属性

axios post请求未映射到Requestbody中的java对象属性
EN

Stack Overflow用户
提问于 2018-09-13 17:14:01
回答 1查看 1.2K关注 0票数 0

我正在开发一个Java Spring引导应用程序,使用React JS作为前端。在我的react js项目中,在表单提交时,我将数据发送到Spring API。由于某些原因,我无法将javascript JSON数组属性映射到java数组属性。除数组外,所有其他字符串数据类型属性都匹配。这是我的例子,在react js前端。

代码语言:javascript
复制
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模型对象:

公共类测试实现了可序列化{

代码语言:javascript
复制
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对象:

代码语言:javascript
复制
@Entity

@Table(name=" PieceData ")公共类PieceData实现了可序列化{

代码语言:javascript
复制
@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;
}

}

EN

回答 1

Stack Overflow用户

发布于 2018-09-14 06:25:29

我通过向List pieceData类型添加json属性注释来解决我的问题:

代码语言:javascript
复制
    @JsonProperty(value = "piecesData")
private List<PieceData> piecesDatas;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52310434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档