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

cgyqldqp  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(65)

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

@Getter
@Setter
@ToString(callSuper = true)
@Entity(name = "organizations")
public class Organization
extends BaseEntity {

    /**
     * The parent organization, if applicable.
     */
    @ManyToOne
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonBackReference
    private Organization parent;

    /**
     * The type of the organization.
     */
    @ManyToOne
    @JoinColumn(name = "organization_type_id", referencedColumnName = "id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @NotNull(message = "organizationType" + AppConstant.NOT_NULL_MESSAGE)
    private OrganizationType organizationType;

    /**
     * The ID of the associated address for this organization.
     */
    @Column(name = "address_id", nullable = false)
    @NotNull(message = "addressId" + AppConstant.NOT_NULL_MESSAGE)
    @Min(value = AppConstant.MIN_ID_VALUE, message = "addressId" + AppConstant.MIN_ID_MESSAGE)
    private Integer addressId;

    /**
     * The name of the organization.
     */
    @Column(name = "name", nullable = false)
    @NotNull(message = "name" + AppConstant.NOT_NULL_MESSAGE)
    @NotEmpty(message = "name" + AppConstant.NOT_EMPTY_MESSAGE)
    @Size(max = AppConstant.MAX_STRING_FIELD_LENGTH, message = "name" + AppConstant.STRING_FIELD_SIZE_MESSAGE)
    private String name;

    /**
     * The phone number associated with this organization.
     */
    @Column(name = "phone_number")
    @Size(max = AppConstant.MAX_STRING_FIELD_LENGTH, message = "phoneNumber" + AppConstant.STRING_FIELD_SIZE_MESSAGE)
    private String phoneNumber;

    @OneToMany(mappedBy = "organization", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonManagedReference("organization-organizationApps")
    private List<OrganizationApp> organizationApps;
}

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

dgenwo3n

dgenwo3n1#

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

相关问题