In this
post, I am going to explain how to handle bidirectional relationships using
Jackson @JsonManagedReference, @JsonBackReference annotations.
Model
classes look like below.
public class User {
private int id;
private String firstName;
private String lastName;
List<DeliveredItem> deliveredItems;
.....
.....
.....
}
public class DeliveredItem {
private int itemId;
private String itemName;
private User deliveredTo;
.....
.....
.....
}
When you
try to Serialize User entity, you will end up in StackOverflowError.
User user1 = new User();
user1.setId(1);
user1.setFirstName("Ram");
user1.setLastName("Gurram");
DeliveredItem item1 = new DeliveredItem();
item1.setDeliveredTo(user1);
item1.setItemId(1);
item1.setItemName("Sofa Set");
user1.setDeliveredItems(new ArrayList () {
{
add(item1);
}
});
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user1);
How to
resolve StackOverflowError?
Solution
1: Using
@JsonManagedReference, @JsonBackReference annotations, we can get resolve these
errors.
@JsonManagedReference
It
represents the forward part of reference. If you annotate any bidirectional
linking field with this annotation, then the field gets serialized normally.
@JsonBackReference
It
represents the back part of reference. If you annotate any bidirectional
linking field with this annotation, then it will be omitted from serialization.
public class User {
private int id;
private String firstName;
private String lastName;
@JsonManagedReference
List<DeliveredItem> deliveredItems;
.....
.....
}
public class DeliveredItem {
private int itemId;
private String itemName;
@JsonBackReference
private User deliveredTo;
.....
.....
}
Find the
below working application.
DeliveredItem.java
package com.sample.app.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
public class DeliveredItem {
private int itemId;
private String itemName;
@JsonBackReference
private User deliveredTo;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public User getDeliveredTo() {
return deliveredTo;
}
public void setDeliveredTo(User deliveredTo) {
this.deliveredTo = deliveredTo;
}
}
User.java
package com.sample.app.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
public class User {
private int id;
private String firstName;
private String lastName;
@JsonManagedReference
List<DeliveredItem> deliveredItems;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public List<DeliveredItem> getDeliveredItems() {
return deliveredItems;
}
public void setDeliveredItems(List<DeliveredItem> deliveredItems) {
this.deliveredItems = deliveredItems;
}
}
App.java
package com.sample.app;
import java.util.ArrayList;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.DeliveredItem;
import com.sample.app.model.User;
public class App {
public static void main(String args[]) throws JsonProcessingException {
User user1 = new User();
user1.setId(1);
user1.setFirstName("Ram");
user1.setLastName("Gurram");
DeliveredItem item1 = new DeliveredItem();
item1.setDeliveredTo(user1);
item1.setItemId(1);
item1.setItemName("Sofa Set");
user1.setDeliveredItems(new ArrayList () {
{
add(item1);
}
});
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user1);
System.out.println(json);
}
}
Output
{"id":1,"firstName":"Ram","lastName":"Gurram","deliveredItems":[{"itemId":1,"itemName":"Sofa
Set"}]}
No comments:
Post a Comment