SpringBoot of solving one, many-to-many and other related entities in JSON serialization / generate infinite recursion when output infinite loop problem (infinite recursion)

Foreword

这问题着实让人苦不堪言,有必要把它记下了。

@JsonBackReference [effective pro-test]

Use annotations @JsonBackReference mark on the physical attributes of the relationship

@Entity
@Table(name = "tb_article")
public class Article {
    /**
     * 文章所在URL
     */
    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false,fetch=FetchType.LAZY)
    @JoinColumn(name = "fk_url_id", referencedColumnName = "pk_url_id", nullable = false)
    @JsonBackReference
    private Url url;

    @ManyToMany
    @JoinTable(name = "r_article_keyword",joinColumns = @JoinColumn(name = "fk_article_id"),
            inverseJoinColumns = @JoinColumn(name = "fk_word_id"))
    @JsonBackReference
    private List<Word> keywords;

    ...
}

Spread

Three Notes

  • @JsonBackReference
  • @JsonManagedReference
  • @JsonIgnore

references

Guess you like

Origin www.cnblogs.com/johnnyzen/p/11079521.html