【问题标题】:Could not write JSON and .JsonMappingException: Infinite recursion (StackOverflowError) error无法写入 JSON 和 .JsonMappingException:无限递归 (StackOverflowError) 错误
【发布时间】:2020-02-12 03:35:05
【问题描述】:

在 Postman 中运行查询时出现 StackOverflow 递归错误。这是模型类:


@Entity
public class UserWallet {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String userName;

    private String firstName;
    private String lastName;
    private String email;

    @Temporal(TemporalType.DATE)

    private Date createdDate;

    @OneToMany(mappedBy = "userAccount", fetch = FetchType.EAGER)

    private Set <Transaction> transactions = new HashSet<>();

交易模型类:


@Entity
public class Transaction {

    @Id
    @GeneratedValue
    private Long id;

    private BigDecimal amount;

    private java.util.Date transactionDate;

    private Long  transactionReference;

    private String details;

    @ManyToOne

    private UserWallet userAccount;

当我在 Postman 上运行查询时,它说:Could not write JSON: Infinite recursion (StackOverflowError);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (通过引用链: java.util.ArrayList[0]->com.Wallet.Model.UserWallet[\"transactions\"])"。

【问题讨论】:

  • 看起来UserWallet正在获取一个事务,它正在获取一个UserWallet,它再次获取事务,形成无限循环,尝试调试调用,看看是否发生这种情况。

标签: java spring spring-boot jpa postman


【解决方案1】:

如果hibernate抛出无限循环stackflow错误,使用@jsonIgnore属性停止序列化。然后在反序列化中JSON字段将被忽略并且不会抛出错误,该属性被忽略并且其值获取其类型的默认值.

点击链接了解更多:Ignore fields from Java object dynamically while sending as JSON from Spring MVC

【讨论】:

  • @wizdemonizer 请批准答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
相关资源
最近更新 更多