I have two objects, Product with OneToMany self reference and OneToMany relationship to Image.
From a Rest API, I need to create CRUD services. 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
{"type":"PNG"}
@Path("/product/{id}/image/add")
@Consumes("application/json")
Update Product:
/product/update/1
{"name":"chair", "description":"blue chair"}
@Path("/product/update/{id}")
@Consumes("application/json")
Update Image:
/image/update/1
{"type":"JPEG"}
@Path("/image/update/{id}")
@Consumes("application/json")
Delete Product:
/product/delete/1
@DELETE
@Path("/product/delete/{id}")
Delete Image:
/image/delete/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;
When I try to add a Product with a Image, I got an error:
{"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; line: 2, column: 27] (through reference chain: br.com.model.Product["images"])
Need help to:
Your images
property in json string is not an array/list, it is an object. It should also be encapsulated with []
to be so, not only with {}
. That's why the deserialization fails.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.