【问题标题】:Direct self-reference leading to cycle Superclass issue JSON直接自引用导致循环超类问题 JSON
【发布时间】:2018-01-24 15:37:51
【问题描述】:

我在搜索时尝试了几件事,但没有任何帮助,或者我没有正确实施。

我得到的错误

Direct self-reference leading to cycle (through reference chain: io.test.entity.bone.Special["appInstance"]->io.test.entity.platform.ApplicationInstance["appInstance"])

这两个都扩展了基础实体,并且在基础(超类)中它也有一个appInstance

基础实体与此类似

@MappedSuperclass
public abstract class BaseEntity implements Comparable, Serializable {

@ManyToOne
protected ApplicationInstance appInstance;

//getter & setter

}

应用程序实体如下所示

public class ApplicationInstance extends BaseEntity implements Serializable { 
   private List<User> users;
// some other properties (would all have the same base and application instance . User entity will look similar to the Special.)
}

特殊实体

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType")
@JsonIgnoreProperties({"createdBy", "appInstance", "lastUpdatedBy"})
public class Special extends BaseEntity implements Serializable {

    @NotNull
    @Column(nullable = false)
    private String name;

    @Column(length = Short.MAX_VALUE)
    private String description;

    @NotNull
    @Column(nullable = false)
    private Double price;

    @OneToOne
    private Attachment image;

    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = SpecialTag.class)
    @CollectionTable(name = "special_tags")
    @Column(name = "specialtag")
    private List<SpecialTag> specialTags;

    @Temporal(TemporalType.TIME)
    private Date specialStartTime;

    @Temporal(TemporalType.TIME)
    private Date specialEndTime;

    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = WeekDay.class)
    @CollectionTable(name = "available_week_days")
    @Column(name = "weekday")
    private List<WeekDay> availableWeekDays;

    @OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
    private List<SpecialStatus> statuses;

    @OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
    private List<SpecialReview> specialReviews;

    @Transient
    private Integer viewed;

    private Boolean launched;

    @OneToMany(mappedBy = "special")
    private List<CampaignSpecial> specialCampaigns;


  @Override
  @JsonIgnore
  public ApplicationInstance getAppInstance() {
    return super.getAppInstance(); 
  }
}

Special 中的所有实体都继承自包含 AppInstance 的 BaseEntity

那我有办法得到特殊的

@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public Special findByGuestRef(@PathParam("ref") String pRefeference) {
  // find the special and return it
 return special;
}

在特殊实体上我尝试了以下

  • 添加了 jsonIgnoreProperties
  • 为 appInstance 添加了一个覆盖以使用 @JsonIgnore 进行注释
  • @JsonIdentityInfo

以上链接

这些解决方案都不起作用。我做错了吗?

注意:是否也可以编辑特殊的,因为其他实体在不同的包中并且不想编辑它们。

【问题讨论】:

  • protected ApplicationInstance appInstance设为私有并为其添加一些getter和setter(受保护的或公共的),然后用@JsonIgnore覆盖ApplicationInstance中的getter注释。
  • @aristotll 我添加了更多细节,但认为不会改变。都引用应用实例
  • 澄清一下:您只能从该实例中排除 appInstance,这就是为什么您不能在映射类上添加 @JsonIgnore
  • @Mark 如有必要,我可以从任何地方排除 appInstance。宁愿避免对 BaseEntity 以及 Appinstance 本身进行更改。 Special 确实包含其他带有 appinstance 的实体,我应该全部排除它吗?
  • 可能是的,但很容易判断您是否可以包含整个实体定义。可以加吗?我也可以自己测试一下。

标签: java json inheritance jax-rs


【解决方案1】:

通常在响应中排除属性就像在其getter中添加@JsonIgnore注解一样简单,但是如果您不想将此注解添加到父类,则可以覆盖getter,然后将注解添加到它:

public class Special extends BaseEntity implements Serializable {
    ...
    @JsonIgnore
    public ApplicationInstance getAppInstance() {
        return this.appInstance;
    }
    ...
}

注意:由于有多个框架,请确保您使用的是正确的@JsonIgnore 注释,否则它将被忽略,例如参见this answer

另一种选择,更“手动”,只是为响应创建一个 bean,这将是 Special 实例的子集:

@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public SpecialDTO findByGuestRef(@PathParam("ref") String pRefeference) {
  // find the special and return it
 return new SpecialDTO(special);
}


public class SpecialDTO {

    //declare here only the attributes that you want in your response

    public SpecialDTO(Special sp) {
        this.attr=sp.attr; // populate the needed attributes
    }

}

【讨论】:

  • 谢谢,手册确实有效,可以使用。将尝试另一种方式来编辑超类,因为我有很多像特殊的实体并且不想为每个实体创建一个 DTO 类。
  • 请告诉我们它是否有效,因为我没有测试过,这只是一个理论上的解决方案
  • 我试过了,还是不行。现在我将使用建议的“手动选项”,该选项目前有效。
  • 同样的错误。如果没有弹出任何内容(到目前为止大部分都是“手动”完成的),将接受这个作为答案。谢谢
  • 最后一次检查:确保您使用的是正确的 @JsonIgnore 注释:stackoverflow.com/questions/19894955/…
【解决方案2】:

对我来说,问题似乎出在 Special 对象和其中正在初始化的字段中。 我猜想在序列化发生时检测到循环引用。 类似于:

class A {
    public A child;
    public A parent;
}

A object = new A();
A root = new A();
root.child = object;
object.parent = root;

在上面的代码中,每当你尝试序列化这些对象中的任何一个时,你都会面临同样的问题。 请注意,不建议使用公共字段。

我建议查看您的 Special 对象和其中设置的引用。

【讨论】:

  • 请阅读整个问题,OP 知道。问题是如何在不修改声明有问题属性的父类的情况下解决这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2018-04-10
  • 1970-01-01
  • 2021-08-28
  • 2018-11-27
  • 1970-01-01
相关资源
最近更新 更多