序列化OrientDB RecordID时无限递归 [英] Infinite recursion when serializing OrientDB RecordID

查看:110
本文介绍了序列化OrientDB RecordID时无限递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的架构中,我有一个这样的抽象类:

In my schema I have an abstract class like this:

@JsonAutoDetect(JsonMethod.NONE)
public abstract class AbstractEntity {

    @Id private Object id;
    @Version private Integer version;

    public AbstractEntity() {}

    @JsonProperty // this annotation causes infinite recursion
    public Object getId() { return id; }

}

此类用作每个实体的超类,示例:

This class is used as superclass for every entity, for example:

@JsonAutoDetect(JsonMethod.NONE)
public class Usuario extends AbstractEntity {

    private Cadastro cadastro;

    protected Usuario() {}

    public Usuario(Cadastro cadastro) {
        setCadastro(cadastro);
    }

    @JsonProperty
    public Cadastro getCadastro() { return cadastro; }

    @JsonProperty
    public void setCadastro(Cadastro cadastro) { this.cadastro = cadastro; }

}

我正在设计一个允许用户使用的REST层通过JSON请求/响应在db中创建记录:

I'm designing a REST layer which will let users create records in db via JSON request/response:

@POST
@Path("cadastrar")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response cadastrar(@Context HttpServletRequest request, Cadastro cadastro) {
    OObjectDatabaseTx bd = (OObjectDatabaseTx) request.getAttribute("object.database.tx");

    try {

        bd.begin();
        Usuario usuario = new Usuario(cadastro);
        usuario = bd.save(usuario);
        bd.commit();

        String json = new ObjectMapper().writeValueAsString(usuario);
        Response response = Response.status(HttpURLConnection.HTTP_CREATED).entity(json).build();

        return response;
        (...)

然而,在最后一种方法中,当我打电话给杰克逊时序列化我新创建的实体(在 writeValueAsString 方法中),我得到一个无限递归:

However, in this last method, when I call Jackson to serialize my newly created entity (in writeValueAsString method), I get an infinite recursion:

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain:
  com.orientechnologies.orient.core.id.ORecordId["record"]->com.orientechnologies.orient.core.record.impl.ODocument["identity"]
->com.orientechnologies.orient.core.id.ORecordId["record"]->com.orientechnologies.orient.core.record.impl.ODocument["identity"]
->com.orientechnologies.orient.core.id.ORecordId["record"]->com.orientechnologies.orient.core.record.impl.ODoc...

我可以通过删除 @JsonProperty 来自 getId 中的方法> AbstractEntity 类。但是,这样 id 属性不会出现在JSON响应。

I can avoid the infinite recursion by removing @JsonProperty from getId method in AbstractEntity class. However, this way the id property won't be present in JSON response.

那么,如何序列化 id 属性?

So, how can I serialize the id property?

谢谢!

推荐答案

Jackson是否支持循环引用?是这样吗?

Does Jackson support circular references? Is that the case?

这篇关于序列化OrientDB RecordID时无限递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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