我正在使用云端点和客观化。但当我试图让实体面临错误时 这是实体
@Entity
public class UserList {
@Id private Long listId;
@Parent private Key<User> userKey;
private String name;
private String listPic = "http://localhost/default"; // There must be a default source for pic
private int memberCount;
public UserList() {
}
public UserList(Key<User> userKey, String name, int memberCount) {
this.userKey = userKey;
this.name = name;
this.memberCount = memberCount;
}
public Long getListId() {
return listId;
}
public void setListId(Long listId) {
this.listId = listId;
}
public Key<User> getUserKey() {
return userKey;
}
public void setUserKey(Key<User> userKey) {
this.userKey = userKey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getListPic() {
return listPic;
}
public void setListPic(String listPic) {
this.listPic = listPic;
}
public int getMemberCount() {
return memberCount;
}
public void setMemberCount(int memberCount) {
this.memberCount = memberCount;
}
}
这是api方法。
public List<UserList> getUserList(@Named("userId") Long userId){
// Get userList where ancestor is userId
return ofy().load().type(UserList.class).ancestor(Key.create(User.class, userId)).list();
}
这是错误。
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: java.util.HashMap["items"]->$Proxy32[0]->org.octabyte.zeem1.Datastore.UserList["userKey"]->com.googlecode.objectify.Key["root"])
请让我知道为什么会出现此错误。一切都很好看。我不知道为什么会收到此错误。你能检查一下吗?
更新
public class User {
@Id private Long userId;
@Index private String username;
@Index private String fullName;
@Index private Long phone;
// constructor and getter setter .....
这是我的User类。用户是UserList
的父级,您可以在UserList类中看到。但我不知道问题是什么。我以前从未见过这个错误。这是怎么回事。
答案 0 :(得分:0)
问题是你正在尝试序列化一个Objectify Key
,它指的是它自己。如果您查看Key
Javadoc,您会注意到getRoot()
方法返回类型Key
。因此,当JSON序列化程序尝试将Key
转换为JSON时,它会注意到它引用自身。要解决此问题,您应该更改getUserKey
:
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
public Key<User> getUserKey() {
return userKey;
}
如果要序列化密钥,则应添加一个返回字符串版本的新方法:
@ApiResourceProperty(name = "userKey")
public String getUserKeyString() {
return userKey.toWebSafeString();
}