美文网首页
Jackson全面解析--注解全讲解三(循环依赖杀手锏 @Jso

Jackson全面解析--注解全讲解三(循环依赖杀手锏 @Jso

作者: 牧羊人刘俏 | 来源:发表于2020-11-27 11:48 被阅读0次

    Jackson在序列化对象的时候,如果对象里面有循环依赖的情况,会报栈溢出,示例如下

        @Getter
        @Setter
        @NoArgsConstructor
        class Boss{
    
            String name;
            String department;
            List<Employee> employees;
        }
    
       @Getter
        @Setter
        @NoArgsConstructor
        class Employee{
    
            String name;
            Boss   boss;
        }
    
    测试代码如下:
    
     @Test
        public void JsonManagedReferenceAndJsonBackReferenceTest() throws Exception{
            CombineJacksonAnnotation.Employee employee1 = new  CombineJacksonAnnotation.Employee();
            employee1.setName("employee1");
    
            CombineJacksonAnnotation.Employee employee2 = new  CombineJacksonAnnotation.Employee();
            employee2.setName("employee2");
    
            CombineJacksonAnnotation.Boss boss = new CombineJacksonAnnotation.Boss();
            boss.setName("boss");
            boss.setDepartment("cto");
            boss.setEmployees(Lists.newArrayList(employee1,employee2));
    
            employee1.setBoss(boss);
            employee2.setBoss(boss);
    
            System.out.println(om.writeValueAsString(boss));
    

    运行测试代码

    image.png

    针对这种情况,Jackson提供了@JsonBackReference,加上此注解的字段,不会被序列化,也就打断了循环依赖,如下

        @Getter
        @Setter
        @NoArgsConstructor
        class Employee{
    
            String name;
            @JsonBackReference
            Boss   boss;
        }
    

    再次运行测试代码,结果如下

    {
      "name" : "boss",
      "department" : "cto",
      "employees" : [ {
        "name" : "employee1"
      }, {
        "name" : "employee2"
      } ]
    }
    

    可以看到,在序列化的时候,成功的将Employee里面的Boss忽略掉了,按照这样的逻辑,貌似@JsonBackReference 与@JsonIgnore很相似,我们可以试验下,将@JsonBackReference替换成@JsonIgnore,如下

        @Getter
        @Setter
        @NoArgsConstructor
        class Employee{
    
            String name;
            @JsonIgnore
            Boss   boss;
        }
    

    再次运行代码,可以得到相同的序列化结果,那么@JsonBackReference与@JsonIgnore的区别在哪里呢,区别主要体现在反序列的时候,我们将上面例子中序列化的结果进行反序列化,看看效果,代码如下

    image.png

    可以看到Employee里面的Boss属性没有被赋值,但是我们将代码修改如下

        @Getter
        @Setter
        @NoArgsConstructor
        class Boss{
    
            String name;
            String department;
            @JsonManagedReference
            List<Employee> employees;
    
    
        }
    
        @Getter
        @Setter
        @NoArgsConstructor
        class Employee{
    
            String name;
            @JsonBackReference
            Boss   boss;
        }
    

    再次进行反序列化,奇迹出现了


    image.png

    当属性分别打上@JsonManagedReference 与 @JsonBackReference时,Jackson会知道这两个属性间有父子关系,反序列化初始化的时候会建立起循环依赖。
    可以说这一对注解是解决父子间循环依赖的利器。

    @JsonIdentityInfo

    @JsonIdentityInfo也可以解决父子之间的依赖关系,但是比上面介绍的两个注解更加的灵活,在上面的两个注解中,我们自己明确类之间的父子关系,但是@JsonIdentityInfo是独立的,解决的是相互之间的依赖关系,没有父子之间的上下关系。
    使用方法如下

       @Getter
        @Setter
        @NoArgsConstructor
        @JsonIdentityInfo(property = "@id",generator = ObjectIdGenerators.IntSequenceGenerator.class)
        class Boss{
    
            String name;
            String department;
            //@JsonManagedReference
            List<Employee> employees;
    
    
        }
    
        @Getter
        @Setter
        @NoArgsConstructor
        @JsonIdentityInfo(property = "@id",generator = ObjectIdGenerators.IntSequenceGenerator.class)
        class Employee{
    
            String name;
            //@JsonBackReference
            Boss   boss;
        }
    测试方法如下
     @Test
        public void JsonIdentityInfoTest() throws Exception{
    
            CombineJacksonAnnotation.Employee employee1 = new  CombineJacksonAnnotation.Employee();
            employee1.setName("employee1");
    
            CombineJacksonAnnotation.Employee employee2 = new  CombineJacksonAnnotation.Employee();
            employee2.setName("employee2");
    
            CombineJacksonAnnotation.Boss boss = new CombineJacksonAnnotation.Boss();
            boss.setName("boss");
            boss.setDepartment("cto");
            boss.setEmployees(Lists.newArrayList(employee1,employee2));
    
            employee1.setBoss(boss);
            employee2.setBoss(boss);
    
            System.out.println(om.writeValueAsString(boss));
    }
    输出如下
    {
      "@id" : 1,
      "name" : "boss",
      "department" : "cto",
      "employees" : [ {
        "@id" : 2,
        "name" : "employee1",
        "boss" : 1
      }, {
        "@id" : 3,
        "name" : "employee2",
        "boss" : 1
      } ]
    }
    
    

    其中@id表明此类的唯一标签名,我们可以使用类已经存在的属性名,如下

    @Getter
        @Setter
        @NoArgsConstructor
        @JsonIdentityInfo(property = "name",generator = ObjectIdGenerators.PropertyGenerator.class)
        class Boss{
    
            String name;
            String department;
            //@JsonManagedReference
            List<Employee> employees;
    
    
        }
    
        @Getter
        @Setter
        @NoArgsConstructor
        @JsonIdentityInfo(property = "name",generator = ObjectIdGenerators.PropertyGenerator.class)
        class Employee{
    
            String name;
            //@JsonBackReference
            Boss   boss;
        }
    输出结果如下
    {
      "name" : "boss",
      "department" : "cto",
      "employees" : [ {
        "name" : "employee1",
        "boss" : "boss"
      }, {
        "name" : "employee2",
        "boss" : "boss"
      } ]
    }
    

    此注解的使用在解决循环解决的时候更加的灵活。

    相关文章

      网友评论

          本文标题:Jackson全面解析--注解全讲解三(循环依赖杀手锏 @Jso

          本文链接:https://www.haomeiwen.com/subject/boqxwktx.html