Empresa与
I need to either create a custom filter or choose the first approach that I depicted.
佩索阿
@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
public class Empresa {
@Id
@GeneratedValue
private Long codigo;
private String razaosocial;
private String cnpj;
@OneToMany(mappedBy = "empresa")
private Set<Pessoa> pessoas;
}
返回我的数据的方法
@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class Pessoa {
@Id
@GeneratedValue
private Long codigo;
private String nome;
private String sexo;
private String tipoPessoa;
@ManyToOne
@JoinColumn(name="codigoempresa")
private Empresa empresa;
private String cpf;
}
在创建OneToMany和ManyToOne映射之前,它已经工作了。我完全恢复了我的json。 现在我收到错误。
堆栈跟踪:
在 com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) 〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) 〜[jackson-databind-2.8.10.jar:2.8.10] at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) 〜[杰克逊 - 数据绑定-2.8.10.jar:2.8.10]
这个堆栈跟踪没什么用,但只是在我的控制台eclipse中显示我。
答案 0 :(得分:2)
您正在获取StackOverflowError。这可能发生的原因之一是无限递归。这正是Jackson试图将您的类反序列化为JSON时发生的情况。您可以使用@JsonIgnoreProperties
@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
public class Empresa {
@Id
@GeneratedValue
private Long codigo;
private String razaosocial;
private String cnpj;
@OneToMany(mappedBy = "empresa")
@JsonIgnoreProperties("empresa")
private Set<Pessoa> pessoas;
}
@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class Pessoa {
@Id
@GeneratedValue
private Long codigo;
private String nome;
private String sexo;
private String tipoPessoa;
@ManyToOne
@JoinColumn(name="codigoempresa")
@JsonIgnoreProperties("pessoas")
private Empresa empresa;
private String cpf;
}