使用Jackson和Spring Boot的JPA实体中的循环引用 [英] Circular reference in entity JPA with Jackson and Spring Boot
问题描述
我有两个Web服务:"Proprietario"和"Veiculo","Proprietario"包含"Veiculo"的列表,"Veiculo"包含"Proprietario".
I have two web services: "Proprietario" and "Veiculo", the "Proprietario" contains a list of "Veiculo" and "Veiculo" contains a "Proprietario".
问题是,当我发出调用"Proprietario"的findAll方法的请求时,尝试进行序列化时,Jackson进入了无限循环抛出异常.当我尝试调用"Veiculo"的findAll方法时,也会发生同样的情况.
The problem is that when I make a request calling the findAll method of "Proprietario", when trying to serialize, Jackson goes into infinite loop throwing exception. The same happens when I try to call the findAll method of "Veiculo".
当我打电话给您致电"Veiculo"的findAll并携带"Proprietario",但不将"Veiculo"列表带入"Proprietario"中时,我希望这样做.与我调用"Proprietario"的findAll方法相反,我想带"Veiculo"列表,但不要将"Proprietario"带到"Veiculo".
I would like it when I call you to call the findAll of the "Veiculo", bring along the "Proprietario", but do not bring the "Veiculo" list inside the "Proprietario". The opposite of when I call the findAll method of "Proprietario", I'd like to bring the "Veiculo" list, but do not bring the "Proprietario" into the "Veiculo".
我尝试使用一些Jackson注释,但是没有一个可以解决双方的冲突.
I tried to use some Jackson annotations, but none solves the conflict on both sides.
@Getter
@Setter
@Entity
@EqualsAndHashCode(of = "id")
public class Veiculo {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@Column(length = 10)
private String placa;
@Column(nullable = false)
private Integer ano;
@ManyToOne
@JoinColumn
private Proprietario proprietario;
}
@Getter
@Setter
@Entity
@EqualsAndHashCode(of = "id")
public class Veiculo {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Long id;
@Column(length = 10)
private String placa;
@Column(nullable = false)
private Integer ano;
@ManyToOne
@JoinColumn
private Proprietario proprietario;
}
推荐答案
尝试使用这两个注释
@JsonManagedReference and @JsonBackReference
Try using these two annotations
@JsonManagedReference and @JsonBackReference
请参见 http://www.baeldung.com/jackson-双向关系和无限递归
这篇关于使用Jackson和Spring Boot的JPA实体中的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!