Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

spring boot jackson infinite recursion

// some solutions:

public class Product {
    @NotNull
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", nullable = false)
    @JsonBackReference
    private Category category;
}

public class Category {
    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "category")
    @JsonManagedReference
    private List<Product> products  = new ArrayList<>();
}

// ______________________________________________

@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class, 
  property = "id")
public class Product {
    ...
}

@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class, 
  property = "id")
public class Category {
    ...
}

//________________________

// Just use @JsonIgnore on one of the sides
Source by pt.stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #spring #boot #jackson #infinite #recursion
ADD COMMENT
Topic
Name
2+1 =