【问题标题】:Circular reference in entity JPA with Jackson and Spring Boot带有 Jackson 和 Spring Boot 的实体 JPA 中的循环引用
【发布时间】:2018-09-16 11:31:03
【问题描述】:

我有两个网络服务:“Proprietario”和“Veiculo”,“Proprietario”包含“Veiculo”列表,“Veiculo”包含“Proprietario”。

问题是,当我发出调用“Proprietario”的 findAll 方法的请求时,在尝试序列化时,Jackson 进入无限循环抛出异常。当我尝试调用“Veiculo”的 findAll 方法时也会发生同样的情况。

当我打电话给你调用“Veiculo”的findAll时,我希望它带有“Proprietario”,但不要将“Veiculo”列表带到“Proprietario”内。与调用“Proprietario”的findAll方法相反,我想将“Veiculo”列表带入,但不要将“Proprietario”带入“Veiculo”。

我尝试使用一些 Jackson 注释,但没有一个可以解决双方的冲突。

@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;
}

【问题讨论】:

    标签: java json spring-boot serialization jackson


    【解决方案1】:

    尝试使用这两个注解 @JsonManagedReference and @JsonBackReference

    http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-04
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多