解决FastJson中"$ref重复引用"的问题,先来看一个例子吧: public static void main(String[] args) { UserGroup userGroup = new UserGroup().setName("UserGroup"); User user = new User("User"); for (int i = 0; i < 3; i++) { userGroup.addUser(user); } Console.log(JSON.toJSONString(userGroup)); } @Data @AllArgsConstructor static class User { private String name; } @Data @Accessors(chain = true) static class UserGroup { private String name; private List<User> users = Lists.newArrayList(); public UserGroup addUser(User user) { this.getUsers().add(user); return this; } } 输出结果: {"name":"UserGroup","users":[{"name":"User"},{"$ref":"$.users[0]"},{"$ref":"$.users[0]"}]} 上面的现象就是将user对象的引用重复使用造成了……

阅读全文