Ken Reid
Ken Reid

Reputation: 609

JSON Jackson Infinite Recursion Error Java

I have seen this error solved with many questions in the past with 2 objects which reference each other, but I can't seem to solve it using these techniques referenced (using this line, mainly):

@JsonIgnoreProperties("day")

My issue is that I have 3 objects which have something of a love triangle going on. Here is (some) of the error:

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) 
(through reference chain: java.util.ArrayList[0]->DataObjects.EmployeeShiftAllocation["shift"]->
DataObjects.Shift["day"]->DataObjects.Day["employeeShiftAllocations"]->
java.util.ArrayList[0]->DataObjects.EmployeeShiftAllocation["shift"]->DataObjects.Shift["day"]->......... etc

So I have an object EmployeeShiftAllocation, which references a Shift, which references a Day, which has a list of EmployeeShiftAllocations.

I applied the aforementioned suggested solution, but it did not solve the problem. I placed that statement before the declaration / initialization of the 3 aforementioned objects.

Any help is appreciated.

Upvotes: 1

Views: 927

Answers (2)

Ken Reid
Ken Reid

Reputation: 609

Managed to fix the issue by using

@JsonIgnore

From the EclipseLink JPA, on the Shift object. This caused the recursive loop to break, solving the issue.

Upvotes: 1

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

you can try using @JsonIdentityInfo which will create a reference for hibernate in the second level of serialization which will break the cycle

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id")

Upvotes: 2

Related Questions