【问题标题】:Direct self-reference leading to cycle直接自引用导致循环
【发布时间】:2019-01-23 06:59:59
【问题描述】:

我正在尝试发送一个请求以取回一个对象数组 - Coupon 当我提交请求时我得到了答案 -

直接自引用导致循环(通过引用链: java.util.HashSet[0] => model.Coupon["emptyCoupon"] => model.Coupon["emptyCoupon"])

model.Coupon 可能解决了这个问题。 如果请求的优惠券不存在,则打算退回空优惠券。

public static final int NO_ID = -1;

private static final Coupon EMPTY_COUPON = new Coupon(NO_ID, null, null, null, NO_ID, NO_ID, null, NO_ID, null);

private long id = NO_ID;
private String title;
private LocalDate startDate;
private LocalDate endDate;
private int amount;
private int category;
private String message;
private double price;
private String image;

public Coupon() {
}

private Coupon(long id, String title, LocalDate start_date, LocalDate end_date, int amount, int category,
        String message, double price, String image) {
    this.id = id;
    this.title = title;
    this.startDate = start_date;
    this.endDate = end_date;
    this.amount = amount;
    this.category = category;
    this.message = message;
    this.price = price;
    this.image = image;
}

public Coupon getEmptyCoupon() {
    return EMPTY_COUPON;
}

在添加EMPTY_COUPON 之前,我对请求没有任何问题。 我想要代码中的emptyCoupon,我很乐意提供帮助

【问题讨论】:

标签: java jersey


【解决方案1】:

由于您使用 Jersey 序列化为 JSON 或 XML,因此您的对象图中可能没有循环。

Jersey 没有像 Jackson 那样的 @JsonBackReference,因此您可以考虑将 EMPTY_COUPON 移动到一个单独的类中(类似于 Constants.java)并从那里获取它。

其他选项是将 @XmlIgnore 添加到您的字段或切换到另一个 JSON 序列化程序,例如 Jackson。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2021-08-28
    • 2018-11-27
    • 2017-07-27
    • 1970-01-01
    相关资源
    最近更新 更多