简体   繁体   English

JSON杰克逊注释被忽略了

[英]JSON Jackson Annotations Ignored

I am using @JsonIgnore and still getting StackoverflowError. 我正在使用@JsonIgnore并仍然获得StackoverflowError。

There's a cycle and the annotation is ignored. 有一个循环,注释被忽略。

@Entity
@NamedQuery(name="Buch.findAll", query="SELECT b FROM Buch v")
public class Buch implements Serializable {
    private static final long serialVersionUID = 1L;

    ...

    //bi-directional many-to-one association to Titel
    @OneToOne(mappedBy="buch")
    @JsonIgnore
    private Titel Titel;

    ...

    @JsonIgnore
    public Titel getTitel() {
        return this.verein;
    }

    @JsonIgnore
    public void setTitel(Titel titel) {
        this.titel= titel;
    }
}

Well there are several problems in your code: 那么你的代码有几个问题:

  • You are using @JsonIgnore annotation with both field and its accessors(getter and setter), you should't do that, mapping only one of them is sufficient. 你正在使用@JsonIgnore注释与字段及其访问器(getter和setter),你不应该这样做,只映射其中一个就足够了。 I suggest you map only the getter method with @JsonIgnore . 我建议你只用@JsonIgnore映射getter方法。
  • Another thing is that your Titel 's getter method is not correctly implemented, it should return Titel field but you are returning this.verein , this is totally wrong and will mess up the logic of your code, you need to correct it. 另一件事是你的Titelgetter方法没有正确实现,它应该返回Titel字段但是你要返回this.verein ,这是完全错误的并且会弄乱代码的逻辑,你需要纠正它。

What I suggest here is to use @JsonIgnore only with the getter method which you need to correct: 我建议这里使用@JsonIgnore和你需要纠正的getter方法:

@JsonIgnore
public Titel getTitel() {
    return this.Titel;
}

I think that this code will not work the title will not set by (jackson ex) because you're making a @JsonIgnore on a setter change it with @JsonSetter and on your getters add @JsonIgnore : 我认为这个代码不起作用标题不会设置(杰克逊ex),因为你在setter上创建一个@JsonIgnore用@JsonSetter改变它并在你的getter上添加@JsonIgnore:

@JsonIgnore
public Titel getTitel() {
    return this.verein;
}

@JsonSetter
public void setTitel(Titel titel) {
    this.titel= titel;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM