Reputation: 159
I have Object which has attribute: List<Node> nodeChild;
When I return response:
return new ResponseEntity<>(myObject, HttpStatus.OK);
In browser I get json with all attributes , but without list nodeChild inside myObject .
Entety:
@Entity
@javax.persistence.Table(name="node")
public class Node implements Serializable {
@Id
@GeneratedValue
@Column(name="id_node")
protected int id_node;
@ManyToOne
@JoinColumn(name="id_parent")
protected Node id_parent;
@Column(name="node_name")
protected String node_name;
@JsonBackReference
@OneToMany(fetch = FetchType.EAGER,mappedBy = "id_parent",cascade = CascadeType.ALL)
protected List<Node> nodeChild;
This is what I got:
{"id_node":2,"id_parent":{"id_node":1,"id_parent":null,"node_name":null},"node_name":null}
In java when I try before return response ... myObject.getNodeChild(); I got the list.
Upvotes: 0
Views: 392
Reputation: 23562
You have to annotate id_parent
with @JsonBackReference
, and nodeChild
list with @JsonManagedReference
, as described here:
@JsonManagedReference is the "forward" part of reference: one that gets serialized normally, and handling of which triggers back-linkage for the other reference. Annotated property can be a bean, array, Collection (List, Set) or Map type, and it must be a bean property (handled by a property of type serialized using BeanSerializer).
@JsonBackReference is the "back" part of reference: it will be omitted from serialization, and re-constructed during deserialization of forward reference. Annotated property must be of bean type.
Upvotes: 1