【问题标题】:Jackson2 many to many deserializationJackson2多对多反序列化
【发布时间】:2013-08-01 07:33:11
【问题描述】:

我有两个类事件和用户,它们具有多对多的关系。

public class Event {

    private int id;

  private List<Users> users;
}

public class User {
        private int id;

      private List<Event> events;
}

我已阅读 @JsonIdentityInfo 注释应该有所帮助,但我看不到这样的示例。

【问题讨论】:

    标签: json jackson


    【解决方案1】:

    您可以这样在UserEvent 两个类中使用@JsonIdentityInfo

    import java.util.List;
    
    import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    import com.fasterxml.jackson.annotation.ObjectIdGenerators;
    
    @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class, property="@UUID")
    public class User
    {
        private int id;
        private List<Event> events;
    
        // Getters and setters
    }
    

    ...和

    import java.util.List;
    
    import com.fasterxml.jackson.annotation.JsonIdentityInfo;
    import com.fasterxml.jackson.annotation.ObjectIdGenerators;
    
    @JsonIdentityInfo(generator = ObjectIdGenerators.UUIDGenerator.class, property="@UUID")
    public class Event
    {
        private int id;
        private List<User> users;
    
        // Getters and setters
    }
    

    您可以酌情使用任何@987654321@s。现在,对应多对多映射的对象的序列化和反序列化将成功:

    public static void main(String[] args) throws IOException
    {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    
        Event event1 = new Event();
        event1.setId(1);
        Event event2 = new Event();
        event2.setId(2);
    
        User user = new User();
        user.setId(10);
    
        event1.setUsers(Arrays.asList(user));
        event2.setUsers(Arrays.asList(user));
        user.setEvents(Arrays.asList(event1, event2));
    
        String json = objectMapper.writeValueAsString(user);
        System.out.println(json);
    
        User deserializedUser = objectMapper.readValue(json, User.class);
        System.out.println(deserializedUser);
    }
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢。我遇到的问题是我收到来自 REST API 调用的字符串。用户对象看起来像 {"_id": "51f1f8acf3ce41b062000005","events": [{"_id":"51f1fccef3ce41b062000007","users": [{"_id": "51f1f8acf3ce41b062000005"}],}]}。所以该请求返回当前用户、事件列表和事件中的用户。我使用了 ObjectIdGenerators.PropertyGenerator.class, property="_id" ,它可以工作。但是由于用户对象_id在事件数组中重复,我得到一个异常已经通过引用链获得了id的POJO:models.User["events"]->models.Event["users"]->models.User["_id "]
    • 嗯。我的答案中的代码示例应该可以正常工作。但是,如果您遇到 ID 属性问题,您可以尝试在 User.javaEvent.java@JsonIdentityInfo 注释中使用不同的 property 值(分别为 property="userId"property="eventId")。
    • toptal.com/javascript/bidirectional-relationship-in-json这篇文章解释了@JsonIdentityInfo注解,你可以参考它得到更多的理解。
    【解决方案2】:

    我来这里是“谷歌搜索”,所以我最终使用了@Jackall 的答案,并带有一个小模组

    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    

    这是因为我的 DTO 有一个名为“id”的属性,例如问题中的 Event 和 User 类。

    【讨论】:

      【解决方案3】:

      尝试在 JsonIdentityInfo 注释中使用“范围”属性:

      @JsonIdentityInfo(
          generator = ObjectIdGenerators.UUIDGenerator.class,   
          property="@UUID",
          scope=YourPojo.class
      )
      

      【讨论】:

      • 范围对 UUIDGenerator 没有影响。从文档中...与其他生成器的一个区别是范围始终设置为 Object.class (无论参数如何):这是因为 UUID 是全局唯一的,并且范围没有意义。
      猜你喜欢
      • 1970-01-01
      • 2022-11-06
      • 2013-05-01
      • 2013-12-04
      • 1970-01-01
      • 2013-04-15
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多