當關系適用于同一個物體時@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);
}
}
"id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2, "mail": "[email protected]", "password": "123456", "followers": [], "following": [ { "id": 1, "mail": "[email protected]", "password": "78824381", "followers": [ { "id": 2,
uj5u.com熱心網友回復:
我認為您的答案在這里https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
注意第 3 段和注釋 @JsonManagedReference 和 @JsonBackReference。
通過使用 JsonManagedReference 可以避免回圈依賴問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/449450.html
