JsonMappingException:如何通过Restlet传输对象化实体(带有键) [英] JsonMappingException: How to transfert a Objectify Entity (with Key) through Restlet

查看:129
本文介绍了JsonMappingException:如何通过Restlet传输对象化实体(带有键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个与GAE服务器+ Objectify DB通讯的Android应用程序.

I am developping an Android Application, comunicating with a GAE server + Objectify DB.

我选择Restlet作为Rest框架.

I choose Restlet for rest framework.

当我尝试检索具有Key属性的实体时遇到问题.服务器抛出错误:

I have a problem when I try to retrieve an Entity with a Key attribute. The server throws an error:

org.codehaus.jackson.map.JsonMappingException: Direct self-reference leading to cycle
(through reference chain: java.util.ArrayList[0]->com.my.model.MyMessage["senderKey"]->com.googlecode.objectify.Key["root"])

这是我的模型(非常简单):

Here is my model (very simple):

public class MyMessage implements Serializable {
private static final long serialVersionUID = -1075184303389185795L;

@Id
private Long id;

@Unindexed
private String sendMessage;

@Parent
Key<MyUser> senderKey;

private MyMessage() {
}

public MyMessage(MyUser user, String message) {
    super();
    this.sendMessage = message;
    this.senderKey = new Key<MyUser>(MyUser.class, user.getId());
}

[... getters and setters ...]
}

.

public class MyUser implements Serializable {

private static final long serialVersionUID = 7390103290165670089L;
@Id private String id;

private MyUser() {
    this.setId("default");
}

public MyUser(String mail) {
    this.setId(mail);
}
[... getters and setters ...]

}

我该怎么办才能解决这个问题?

What can I do to solve this problem??

推荐答案

Objectify的键具有便捷的方法getRoot(),该方法返回祖先链中的根元素.当键的根时,getRoot()返回 this .杰克逊将其检测为周期,因此您会得到错误.

Objectify's Key has a convenience method getRoot() that returns the root element in the ancestor chain. When a key is the root, getRoot() returns this. Jackson detects this as a cycle and thus you get the error.

处理此问题的最佳方法是不要尝试将Keys序列化为json对象.密钥在JSON中更好地表示为字符串化版本.

The best way to deal with this is not to try to serialize Keys as json objects. Keys are much better represented in JSON as the stringified version.

我不知道您如何在Restlet中执行此操作,但是您需要修改ObjectMapper实例以提供执行此转换的KeySerializer.查看Objectify4中的ObjectifyJacksonModule以获取指导:

I don't know how you do this in Restlet, but you need to modify the ObjectMapper instance to provide a KeySerializer that does this translation. Look at the ObjectifyJacksonModule in Objectify4 for guidance:

https://code.google.com/p/objectify-appengine/source/browse/src/com/googlecode/objectify/util/jackson/ObjectifyJacksonModule.java

这篇关于JsonMappingException:如何通过Restlet传输对象化实体(带有键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆