Jackson JSON annotations ignored

I use @JsonIgnore and still get a StackoverflowError.

There is a loop, 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;
    }
}
+4
source share
2 answers

There are several problems in the code:

  • You use annotation @JsonIgnorewith both fields and accessors (getter and setter), you should not do this, displaying only one of them is enough. I suggest you display only the getter method with @JsonIgnore.
  • Another thing is that your method is Titel getterincorrect, it must return a field Titel, but you are returning this.verein, it is completely wrong and will ruin the logic of your code, you must fix it.

@JsonIgnore getter, :

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

, , (jackson ex), @JsonIgnore @JsonSetter, getters @JsonIgnore:

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

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

Source: https://habr.com/ru/post/1675848/


All Articles