如何隐藏“孩子”:[]从弹簧靴中一对多关系中的最后一个孩子

Posted

技术标签:

【中文标题】如何隐藏“孩子”:[]从弹簧靴中一对多关系中的最后一个孩子【英文标题】:How to hide "children": [] from last child in one to many relationship in spring boot 【发布时间】:2021-04-16 19:15:27 【问题描述】:

实际上我想为我的反应原生应用程序使用树 Json 响应。所以我在spring boot中构建它。但是由于 "children": [] in last child 我在反应原生时遇到了一些问题。所以我想在我的回复中隐藏它。

1.我收到了这样的回复

[
            "id": 1,
            "name": "Martin",                
            "haveAccount": false,
            "gender": "m",
            "children": [
                
                    "id": 4,
                    "name": "Werite",
                    "haveAccount": false,
                    "gender": "f",
                    "children": []
                
                
            ]
        ]

2.但我不想要 "children": [] in last child

例子:-

    [
                "id": 1,
                "name": "Martin",                
                "haveAccount": false,
                "gender": "m",
                "children": [
                    
                        "id": 4,
                        "name": "Werite",
                        "haveAccount": false,
                        "gender": "f"
                    
                    
                ]
            ]


3.这是我的实体类

@Service
@Entity
@Table
public class Family    
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
@Column(name="Family_id")
private Integer id;
private String name;
private String village;
private String wifeName;
private String talukaName;
private Boolean haveAccount;
private String username;
private String gender;
private String mobileNumber;




@OneToOne
@JoinColumn(name = "parent_id")
@JsonBackReference

private Family parent;

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Family> children = new ArrayList<>();


public Family(Integer id, String name, String village, String wifeName, String talukaName, Boolean haveAccount,
        String username, String gender, String mobileNumber, Family parent, List<Family> children) 
    super();
    this.id = id;
    this.name = name;
    this.village = village;
    this.wifeName = wifeName;
    this.talukaName = talukaName;
    this.haveAccount = haveAccount;
    this.username = username;
    this.gender = gender;
    this.mobileNumber = mobileNumber;
    this.parent = parent;
    this.children = children;

public Family() 
super();
    


public Integer getId() 
    return id;


public void setId(Integer id) 
    this.id = id;


public String getName() 
    return name;


public void setName(String name) 
    this.name = name;


public Family getParent() 
    return parent;


public void setParent(Family parent) 
    this.parent = parent;


public List<Family> getChildren() 
    return children;


public void setChildren(List<Family> children) 
    this.children = children;


public void addChild(Family children) 
    this.children.add(children);


public String getVillage() 
    return village;


public void setVillage(String village) 
    this.village = village;


public String getWifeName() 
    return wifeName;


public void setWifeName(String wifeName) 
    this.wifeName = wifeName;


public String getTalukaName() 
    return talukaName;


public void setTalukaName(String talukaName) 
    this.talukaName = talukaName;


public Boolean getHaveAccount() 
    return haveAccount;


public void setHaveAccount(Boolean haveAccount) 
    this.haveAccount = haveAccount;


public String getUsername() 
    return username;


public void setUsername(String username) 
    this.username = username;


public String getMobileNumber() 
    return mobileNumber;


public void setMobileNumber(String mobileNumber) 
    this.mobileNumber = mobileNumber;


public String getGender() 
    return gender;


public void setGender(String gender) 
    this.gender = gender;

【问题讨论】:

【参考方案1】:

如果你想删除一个字段,你可以把它设为 null 并在你的类上使用@JsonInclude(Include.NON_NULL),如下所示:

@Service
@Entity
@Table
@JsonInclude(Include.NON_NULL)
public class Family   
  //class

如果您可以处理,您应该将private List&lt;Family&gt; children = new ArrayList&lt;&gt;(); 替换为private List&lt;Family&gt; children;,并在您打算使用它时对其进行初始化。

只是一个问题:你为什么在你的实体类上使用@Service?

【讨论】:

感谢您的帮助。您的回答帮助我解决了我的疑问。但是使用@JsonInclude(Include.NON_NULL) 的瞬间,对于这个代码@JsonInclude(Include.NON_EMPTY) 是正确的解决方案,这对我有用。 是的,NON_EMPTY 是一个很好的解决方案,但如果你不需要它,你应该避免初始化空列表。这就是为什么我建议你删除课堂上的初始化。无论如何,如果我有任何帮助,我很高兴。 :D

以上是关于如何隐藏“孩子”:[]从弹簧靴中一对多关系中的最后一个孩子的主要内容,如果未能解决你的问题,请参考以下文章

弹簧靴中的 SSEemitter vs Flux

观察后如何在弹簧靴中重置仪表指标

如何在百里香弹簧靴中填充选择元素?

在百里香弹簧靴中,动态旋转木马滑块不起作用

Mysql 一对多关系建立(在navicat中)

防止一对多关系中的额外外键列