[英]Deserializing JSON into Object with list attribute
I have two objects, Product with OneToMany self reference and OneToMany relationship to Image. 我有两个对象,具有OneToMany自我引用的Product和与Image的OneToMany关系。
From a Rest API, I need to create CRUD services. 从Rest API,我需要创建CRUD服务。 I created the following services:
我创建了以下服务:
Add new Product: {"name":"chair", "description":"red chair"} 添加新产品:{“名称”:“椅子”,“描述”:“红色椅子”}
@Path("/product/add")
@Consumes("application/json")
Add new Child Product: {"name":"table", "description":"red table"} 添加新的子产品:{“名称”:“表”,“描述”:“红色表”}
@Path("/product/{id}/addChild")
@Consumes("application/json")
Add Image to Product: 将图像添加到产品:
/product/1/image/add /产品/ 1 /图像/添加
{"type":"PNG"} { “类型”: “PNG”}
@Path("/product/{id}/image/add")
@Consumes("application/json")
Update Product: 更新产品:
/product/update/1 /产品/更新/ 1
{"name":"chair", "description":"blue chair"} {“名称”:“椅子”,“描述”:“蓝色椅子”}
@Path("/product/update/{id}")
@Consumes("application/json")
Update Image: 更新图片:
/image/update/1 /图像/更新/ 1
{"type":"JPEG"} { “类型”: “JPEG”}
@Path("/image/update/{id}")
@Consumes("application/json")
Delete Product: 删除产品:
/product/delete/1 /产品/删除/ 1
@DELETE
@Path("/product/delete/{id}")
Delete Image: 删除图片:
/image/delete/1 /图像/删除/ 1
@DELETE
@Path("/image/delete/{id}")
Product.java: Product.java:
@Entity
@Table
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer productId;
@Column(name = "NAME")
private String name;
@Column(name = "DESCRIPTION")
private String description;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="PRODUCT_ID")
private List<Image> images;
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
private List<Product> children;
@ManyToOne
@JoinColumn(name = "PARENT_PRODUCT_ID")
private Product parent;
Image.java: Image.java:
@Entity
@Table
public class Image implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer imageId;
@Column(name = "TYPE")
private String type;
@ManyToOne
@JoinColumn(name = "PRODUCT_ID", nullable = false)
private Product product;
When I try to add a Product with a Image, I got an error: 当我尝试添加带有图片的产品时,出现错误:
{"name":"bucket", "description":"red bucket", "images":{"type":"jpeg"} } {“ name”:“ bucket”,“ description”:“ red bucket”,“ images”:{“ type”:“ jpeg”}}
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3c9786ad;
无法在[来源:org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3c9786ad;中从START_OBJECT令牌中反序列化java.util.ArrayList实例。 line: 2, column: 27] (through reference chain: br.com.model.Product["images"])
第2行,第27列](通过参考链:br.com.model.Product [“ images”])
Need help to: 需要帮助:
Your images
property in json string is not an array/list, it is an object. 您在json字符串中的
images
属性不是数组/列表,而是一个对象。 It should also be encapsulated with []
to be so, not only with {}
. 也应使用
[]
封装它,不仅要用{}
封装。 That's why the deserialization fails. 这就是反序列化失败的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.