Search code examples
javagwtobjectmapperjson-serialization

JsonMappingException: Direct self-reference leading to cycle (through reference chain: MyClass["underlyingValue"])


I have a POJO class extending net.sf.gilead.pojo.gwt.LightEntity. I am not able to serialize the POJO object to JSON string using com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(obj). I am getting this error

com.fasterxml.jackson.databind.JsonMappingException: 
Direct self-reference leading to cycle 
(through reference chain: com.example.MyClass["underlyingValue"])

Noticed that the LightEntity class has this method:

public Object getUnderlyingValue() {
   return this;
}

Solution

  • How about to try to override this method in your MyClass class and add the @JsonIgnore annotation?

    Example:

    public class MyClass extends LightEntity {
    
        @JsonIgnore
        @Override
        public Object getUnderlyingValue() {
            return super.getUnderlyingValue();
        }
    
    }