使用jackson序列化TreeModel

时间:2013-02-18 10:14:56

标签: java json spring-mvc jackson

我有以下型号:

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

3 个答案:

答案 0 :(得分:6)

正如 drorb 所提到的,循环引用就是问题所在。但是,不应使用@JsonIgnore注释,而应使用@JsonBackReference来指示循环引用。使用此注释,Jackson可以在反序列化时映射到正确的父级。

答案 1 :(得分:0)

模型和model1(父/子)之间的模型中有一个循环引用,它不会由jackson自动处理。 您可以通过使用@JsonIgnore注释标记要忽略的引用之一来解决此问题。 您可以在此article

中找到更多信息

答案 2 :(得分:0)

长话短说,我已经使用了以下建议

Summary.java
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "summary")
@JsonManagedReference
private List<Section> issueSections;

Section.java
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
@JoinColumn(name = "SUMMARY_ID", updatable = false)
private Summary summary;
这解决了我的问题。