@JsonIgnore 在正常关系下可以正常工作,但是当我将多对多应用于同一实体时,它不起作用

@JsonIgnore works correctly with normal relations but when i apply a many to many to the same entity it doesn't work

@JsonIgnore 在关系适用于同一实体时不起作用,但在与不同实体的关系中使用时它工作正常我想用它来停止 json 的递归问题是有一种方法可以解决此问题或使用与@JsonIgnore 不同的方法,如果有人知道答案,请与我分享问题的原因,以便我将来避免这种情况。 提前谢谢你。

    package com.socialapp.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(unique = true,name = "username")
    private String Username;

    @Column(unique = true,name = "mail")
    private String mail;

    @Column(name = "password")
    private String password;

    @JsonIgnore //this is a one to many relationship with entity post works fine
    @OneToMany(mappedBy = "poster")
    private Set<Post>  posts= new HashSet<>();

    @JsonIgnore //this is a many to many relationship with entity post works fine
    @ManyToMany(mappedBy = "likers")
    private Set<Post> postsLiked = new HashSet<>();


    @ManyToMany
    @JoinTable(
            name = "follow",
            joinColumns = @JoinColumn(name = "followers"),
            inverseJoinColumns = @JoinColumn(name = "following")
    )
    private Set<User> Followers = new HashSet<>();

    @JsonIgnore  //this is a many to many relationship with the same entity @jsonignore doesn't work
    @ManyToMany(mappedBy = "Followers")
    private Set<User> Following = new HashSet<>();


    public User(String username, String mail, String password) {
        Username = username;
        this.mail = mail;
        this.password = password;
    }

    public User() {
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return Username;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public Set<Post> getPosts() {
        return posts;
    }

    public void setPosts(Set<Post> posts) {
        this.posts = posts;
    }

    public Set<Post> getPostsLiked() {
        return postsLiked;
    }

    public void setPostsLiked(Set<Post> postsLiked) {
        this.postsLiked = postsLiked;
    }

    public Set<User> getFollowers() {
        return Followers;
    }

    public void setFollowers(Set<User> followers) {
        Followers = followers;
    }

    public Set<User> getFollowing() {
        return Following;
    }

    public void setFollowing(Set<User> following) {
        Following = following;
    }
    public void addFollower(User following){
        this.Followers.add(following);
    }
}

"编号": 1, "邮件": "ziadhowayek12@gmail.com", “密码”:“78824381”, “追随者”:[ { “编号”:2, "邮件": "jadhowayek@gmail.com", “密码”:“123456”, “追随者”:[], “下列的”: [ { “编号”:1, "邮件": "ziadhowayek12@gmail.com", “密码”:“78824381”, “追随者”:[ { “编号”:2, "邮件": "jadhowayek@gmail.com", “密码”:“123456”, “追随者”:[], “下列的”: [ { “编号”:1, "邮件": "ziadhowayek12@gmail.com", “密码”:“78824381”, “追随者”:[ { “编号”:2, "邮件": "jadhowayek@gmail.com", “密码”:“123456”, “追随者”:[], “下列的”: [ { “编号”:1, "邮件": "ziadhowayek12@gmail.com", “密码”:“78824381”, “追随者”:[ { “编号”:2, "邮件": "jadhowayek@gmail.com", “密码”:“123456”, “追随者”:[], “下列的”: [ { “编号”:1, "邮件": "ziadhowayek12@gmail.com", “密码”:“78824381”, “追随者”:[ { “编号”:2,

我想你的答案就在这里https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

注意第 3 段和注释 @JsonManagedReference 和 @JsonBackReference。

通过使用 JsonManagedReference 可以避免循环依赖问题。