我有以下模型:
public class CategoryModel {
public String id;
public String name;
public String imageUrl;
public CategoryModel parent;
public List<CategoryModel> childrens = new ArrayList<CategoryModel>();
}
我正在使用 Spring MVc + jackson 来返回 Json。
我的操作构建了以下模型,执行后,我得到了 stackoverflow 异常。
List<CategoryModel> allCategories = new ArrayList<CategoryModel>();
CategoryModel model = new CategoryModel();
model.id = "1";
model.name = "category 1";
CategoryModel model1 = new CategoryModel();
model.id = "2";
model.name = "category 1.1";
model.childrens.add(model1);
model1.parent = model;
allCategories.add(model);
知道我有什么问题吗?
这是日志文件(它的一部分):
在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 在 org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 在 org.codehaus.jackson .map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 在 org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122) 在 org.codehaus.jackson.map.ser .std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71) 在 org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) 在 org.codehaus.jackson.map.ser.BeanPropertyWriter org.codehaus.jackson.map.ser.std.BeanSerializerBase 中的 .serializeAsField(BeanPropertyWriter.java:446)。在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 的 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 的序列化字段(BeanSerializerBase.java:150)在 org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 在 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 在 org.codehaus.jackson .map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122) at org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71) at org.codehaus.jackson .map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) 在 org.codehaus.jackson.map.ser。BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 在 org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 在 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer. java:112) 在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 在 org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 在 org .codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 在 org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122) 在 org.codehaus.jackson .map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71) 在 org.codehaus。jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) 在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 在 org.codehaus.jackson.map.ser。 std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 在 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 在 org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter. java:446) 在 org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 在 org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 在 org .codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122) 在 org.codehaus.jackson.map.ser.std。StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71) 引起:java.lang.StackOverflowError