关于双向多对一的@JsonIgnore注解使用方法

来源:互联网 发布:gta5捏脸数据男小唐尼 编辑:程序博客网 时间:2024/05/18 00:54

问题描述:若不使用@JsonIgnore注解,通过SpringMvc作为Json返回页面时,会出现无限循环的错误。因为一方中拼接了多方,多方又拼接了一方,循环下去。


下面是一个自关联的目录类,用来当作示例:
    @ManyToOne    @JoinColumn(name = "pCode")    @JsonIgnore    private Dictionary parent;    @OneToMany(mappedBy = "parent")    private Set<Dictionary> children = new HashSet<>();

将注解加在parent上,在返回该parent至页面时,会将children也json化一起返回页面,一般来说这是我们需要的效果。


    @ManyToOne    @JoinColumn(name = "pCode")    private Dictionary parent;    @OneToMany(mappedBy = "parent")    @JsonIgnore    private Set<Dictionary> children = new HashSet<>();

若如上面配置,将注解放在children上,则将parent返回页面时,不会将其对应的children进行json化,反之,若将children返回页面,则每个children中都会将parent进行一次json化,这样不符合一般的开发规律。


原创粉丝点击