2

可能是我在杰克逊遗漏了一个明显的注释,但我有以下课程

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.jsonschema.JsonSchema;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Loop {

  private String name;
  private Loop otherLoop;

  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
  public Loop getOtherLoop() { return otherLoop; }
  public void setOtherLoop(Loop otherLoop) { this.otherLoop = otherLoop; }

  public static void main(String[] args) throws Exception {
    Loop parent = new Loop();
    parent.setName("parent");

    Loop child = new Loop();
    child.setName("child");
    child.setOtherLoop(parent);

    ObjectMapper mapper = new ObjectMapper();
    System.out.println(mapper.writeValueAsString(child));

    JsonSchema jsonSchema = mapper.generateJsonSchema(Loop.class);
    System.out.println(mapper.writeValueAsString(jsonSchema));
  }
}

当我使用 Jackson 2 运行它时,它会进入无限循环

{"name":"child","otherLoop":{"name":"parent"}}
Exception in thread "main" java.lang.StackOverflowError
at com.fasterxml.jackson.databind.cfg.MapperConfig.isEnabled(MapperConfig.java:106)
at com.fasterxml.jackson.databind.SerializationConfig.getAnnotationIntrospector(SerializationConfig.java:382)
at com.fasterxml.jackson.databind.SerializerProvider.getAnnotationIntrospector(SerializerProvider.java:307)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.createContextual(BeanSerializerBase.java:318)
at com.fasterxml.jackson.databind.SerializerProvider._handleContextual(SerializerProvider.java:971)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:447)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:619)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:621)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:621)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:621)

任何想法,解决方法?

4

1 回答 1

0
@XmlAccessorType(XmlAccessType.FIELD)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Loop {
   @XMLAttribute(name = "Name")
   private String name;
   @XMLAttribute(name = "Loops")
   private List<Loop> listLoop;

   //getters and setters    
}
于 2012-08-21T13:09:30.880 回答