Infinite recursion when serializing OrientDB RecordID

有些话、适合烂在心里 提交于 2020-02-02 10:21:16

问题


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; }

}

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;
        (...)

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...

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.

So, how can I serialize the id property?

Thanks!


回答1:


Does Jackson support circular references? Is that the case?



来源:https://stackoverflow.com/questions/14835800/infinite-recursion-when-serializing-orientdb-recordid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!