Spring Boot 无法写入JSON:无限递归(StackOverflowError)]

cgyqldqp  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(170)

我没有在响应中获取“parent”字段。如果我删除@JsonBackReference注解,递归将开始。我需要在不递归的情况下获取父对象1次。

  1. @Getter
  2. @Setter
  3. @ToString(callSuper = true)
  4. @Entity(name = "organizations")
  5. public class Organization
  6. extends BaseEntity {
  7. /**
  8. * The parent organization, if applicable.
  9. */
  10. @ManyToOne
  11. @JoinColumn(name = "parent_id", referencedColumnName = "id")
  12. @OnDelete(action = OnDeleteAction.CASCADE)
  13. @JsonBackReference
  14. private Organization parent;
  15. /**
  16. * The type of the organization.
  17. */
  18. @ManyToOne
  19. @JoinColumn(name = "organization_type_id", referencedColumnName = "id", nullable = false)
  20. @OnDelete(action = OnDeleteAction.CASCADE)
  21. @NotNull(message = "organizationType" + AppConstant.NOT_NULL_MESSAGE)
  22. private OrganizationType organizationType;
  23. /**
  24. * The ID of the associated address for this organization.
  25. */
  26. @Column(name = "address_id", nullable = false)
  27. @NotNull(message = "addressId" + AppConstant.NOT_NULL_MESSAGE)
  28. @Min(value = AppConstant.MIN_ID_VALUE, message = "addressId" + AppConstant.MIN_ID_MESSAGE)
  29. private Integer addressId;
  30. /**
  31. * The name of the organization.
  32. */
  33. @Column(name = "name", nullable = false)
  34. @NotNull(message = "name" + AppConstant.NOT_NULL_MESSAGE)
  35. @NotEmpty(message = "name" + AppConstant.NOT_EMPTY_MESSAGE)
  36. @Size(max = AppConstant.MAX_STRING_FIELD_LENGTH, message = "name" + AppConstant.STRING_FIELD_SIZE_MESSAGE)
  37. private String name;
  38. /**
  39. * The phone number associated with this organization.
  40. */
  41. @Column(name = "phone_number")
  42. @Size(max = AppConstant.MAX_STRING_FIELD_LENGTH, message = "phoneNumber" + AppConstant.STRING_FIELD_SIZE_MESSAGE)
  43. private String phoneNumber;
  44. @OneToMany(mappedBy = "organization", cascade = CascadeType.ALL, orphanRemoval = true)
  45. @JsonManagedReference("organization-organizationApps")
  46. private List<OrganizationApp> organizationApps;
  47. }

字符串
我试过@JsonIdentityInfo,@JsonManagedReference,@JsonBackReference。在@JsonIdentityInfo中,它在返回单个对象1次后返回ID,并且没有将其正确放置在节点树中。

dgenwo3n

dgenwo3n1#

在我看来,在自引用关系中,使用@JsonIdentityInfo来获取ID值似乎是最好的方法。

相关问题