我有两个对象,Product with OneToMany self reference和OneToMany与Image的关系。
从Rest API,我需要创建CRUD服务。我创建了以下服务:
添加新产品: { “名”: “椅子”, “description”:“红色椅子”}
@Path("/product/add")
@Consumes("application/json")
添加新的儿童产品: { “名”: “表”, “description”:“red table”}
@Path("/product/{id}/addChild")
@Consumes("application/json")
将图像添加到产品:
/产品/ 1 /图像/添加
{ “类型”: “PNG”}
@Path("/product/{id}/image/add")
@Consumes("application/json")
更新产品:
/产品/更新/ 1
{ “名称”: “椅子”, “description”:“蓝色椅子”}
@Path("/product/update/{id}")
@Consumes("application/json")
更新图片:
/图像/更新/ 1
{ “类型”: “JPEG”}
@Path("/image/update/{id}")
@Consumes("application/json")
删除产品:
/产品/删除/ 1
@DELETE
@Path("/product/delete/{id}")
删除图片:
/图像/删除/ 1
@DELETE
@Path("/image/delete/{id}")
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:
@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;
当我尝试添加带有图像的产品时,出现错误:
{ “名称”: “桶”, “描述”:“红桶”, “图像”:{ “类型”: “JPEG”} }
无法从START_OBJECT标记中反序列化java.util.ArrayList的实例 在[来源:org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3c9786ad; line:2,column:27](通过参考链:br.com.model.Product [“images”])
需要帮助:
答案 0 :(得分:0)
json字符串中的images
属性不是数组/列表,它是一个对象。它也应该用[]
封装,不仅仅是{}
。这就是反序列化失败的原因。