【问题标题】:JsonManagedReference vs JsonBackReferenceJsonManagedReference 与 JsonBackReference
【发布时间】:2015-09-27 22:32:14
【问题描述】:

我想知道杰克逊的@JsonManagedReference@JsonBackReference 之间的区别?

【问题讨论】:

    标签: java jackson


    【解决方案1】:

    正如 Rajat Verma 所写,他的解决方案非常有效。谢谢你为我节省了很多时间和愤怒:-)

    重要部分:
    您需要将字段定义为List,我之前将其定义为Set,并且此解决方案不起作用(显示为无限循环)!

    我添加我的解决方案:

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
    public class Agent {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @ManyToMany(mappedBy = "subscribers")
        @ApiModelProperty(dataType = "List", example = "[1,2,3]") // for Swagger
        @JsonIdentityReference(alwaysAsId = true) // show only id of Topic
        private final List<Topic> subscribeTopics = new ArrayList<>()
    }
    
     @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
     public class Topic {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
        @JoinTable(name = "topic_agent",
            joinColumns = @JoinColumn(name = "fk_topic_id"),
            inverseJoinColumns = @JoinColumn(name = "fk_agent_id"))
        @ApiModelProperty(dataType = "List", example = "[1,2,3]")
        @JsonIdentityReference(alwaysAsId = true)
        private final List<Agent> subscribers = new ArrayList<>();
     }
    

    【讨论】:

      【解决方案2】:

      我更喜欢
      @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Long.class)
      其中属性是主键字段的名称,范围是它的类型

      【讨论】:

        【解决方案3】:

        @JsonManagedReference 是引用的前向部分——即 正常序列化。 @JsonBackReference 是后面的部分 参考 - 它将从序列化中省略。

        所以他们真的取决于你们关系的方向

        public class User {
            public int id;
            public String name;
        
            @JsonBackReference
            public List<Item> userItems; 
        } 
        
        public class Item {
            public int id;
            public String itemName;
        
            @JsonManagedReference
            public User owner; 
         }
        

        【讨论】:

        【解决方案4】:
        • @JsonManagedReference -> 管理引用的前向部分,该注释标记的字段是序列化的字段
        • @JsonBackReference -> 管理引用的反向部分,并且使用此注释标记的字段/集合未序列化。

        用例: 您的实体/表中有一对多或多对多关系,不使用上述内容会导致错误,如

        Infinite Recursion and hence stackoverflow - > Could not write content: Infinite recursion (StackOverflowError)
        

        出现上述错误是因为 Jackson(或其他类似者)试图序列化关系的两端并以递归结束。

        @JsonIgnore 执行类似的功能,但上述注释更可取。

        【讨论】:

          【解决方案5】:

          @JsonManagedReference@JsonBackReference 旨在处理字段之间的这种双向链接,一种用于父角色,另一种用于子角色。

          为了避免这个问题,链接被处理使得属性 带有@JsonManagedReference 注释的注释被正常处理 (正常序列化,反序列化没有特殊处理)和 使用 @JsonBackReference 注释注释的属性不是 序列化;并且在反序列化期间,其值设置为实例 具有“托管”(转发)链接。

          【讨论】:

          • 如果@JsonBackReference注解没有被序列化,那么它如何在没有被序列化的情况下被“反序列化”呢?
          猜你喜欢
          • 2016-09-20
          • 1970-01-01
          • 2020-04-23
          • 2016-11-07
          • 2021-11-15
          • 2021-12-20
          • 2018-05-23
          • 1970-01-01
          • 2018-04-15
          相关资源
          最近更新 更多