直接自引用导致循环异常

时间:2013-11-26 13:36:02

标签: java serialization elasticsearch jackson deserialization

我有类似这样的课程

public abstract class ElasticSearchValue<T> {

  private Long txId;
  private Long currentTxId;
  private T previous;

  public Long getTxId() {
    return txId;
  }

  public void setTxId(Long txId) {
    this.txId = txId;
  }

  public Long getCurrentTxId() {
    return currentTxId;
  }

  public void setCurrentTxId(Long currentTxId) {
    this.currentTxId = currentTxId;
  }

  public Object getPrevious() {
    return previous;
  }

  public void setPrevious(T previous) {
    this.previous = previous;
  }

}

一个扩展上面课程的课程

public class DailyActivity extends ElasticSearchValue<DailyActivity> {

  Long agentId;
  Date date;
  Long success;

  public Long getAgentId() {
    return agentId;
  }

  public void setAgentId(Long agentId) {
    this.agentId = agentId;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public Long getSuccess() {
    return success;
  }

  public void setSuccess(Long success) {
    this.success = success;
  }

  @Override
  public String toString() {
    return agentId + "_" + date.toString();
  }

}

现在,我有一个DailyActivity类型的对象,当我尝试将其转换为json字符串时,我得到以下异常:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.pr.analysis.DailyActivity["previous"])

我在google上寻找解决方案,但我得到的解决方案要求将jsonIgnore放到之前的值,这不是我打算做的。有没有人遇到同样的问题? 感谢

修改 我知道课堂上有一个循环,我问如何反序列化有自引用的类?

6 个答案:

答案 0 :(得分:11)

在这种情况下,您需要注释与@JsonManagedReference@JsonBackReference的关系,如下所示:

 @ManyToOne
 @JoinColumn(name = "company_id", referencedColumnName = "id")
 @JsonBackReference
 private Company company 

并且

 @OneToMany(mappedBy="company")
 @JsonManagedReference
 private Set<Employee> employee = new HashSet<Employee>();

有一个很好的例子here

答案 1 :(得分:3)

自我引用在这里:

public class DailyActivity extends ElasticSearchValue<DailyActivity> {

你说DailyActivity是一个ElasticSearchValue<DailyActivity>,它本身就是一个ElasticSearchValue<ElasticSearchValue<DailyActivity>>,这无限地继续......

更新:我会在两个课程中打破这个。创建DailyActivity,不要继承ElasticSearchValue

public class DailyActivity {
  // the same content as your class above
然后创建另一个类,如:

public class ElacticDailyActivity extends ElasticSearchValue<DailyActivity> {

答案 2 :(得分:1)

尝试此示例中给出的@JsonIdentityInfo注释。更多细节http://wiki.fasterxml.com/JacksonFeatureObjectIdentity

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class Identifiable
{
    public int value;

    public Identifiable next;
}

答案 3 :(得分:1)

SerializationFeature有一个名为FAIL_ON_SELF_REFERENCES的属性,默认值为true,但是您可以设置为false来跳过这种异常。

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);

如果您使用的是SpringBoot,则只需在spring.jackson.serialization.fail-on-self-references=false 中添加application.properties

答案 4 :(得分:0)

我认为请参考相关问题和合理的处理方式。 https://stackoverflow.com/a/65920426/12594701

在当前情况下,像下面这样注释“上一个”会有所帮助

@JsonIgnoreProperties({"previous"})//... along with other annotations
private T previous;

答案 5 :(得分:-1)

我想你代码中的某个地方。 DailyActivity的某个实例的前一个指向自己。