gson.toJson() 抛出 StackOverflowError [英] gson.toJson() throws StackOverflowError
问题描述
我想从我的对象生成一个 JSON 字符串:
I would like to generate a JSON String from my object:
Gson gson = new Gson();
String json = gson.toJson(item);
每次我尝试这样做时,我都会收到此错误:
Everytime I try to do this, I get this error:
14:46:40,236 ERROR [[BomItemToJSON]] Servlet.service() for servlet BomItemToJSON threw exception
java.lang.StackOverflowError
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:473)
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:347)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:440)
at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:235)
at com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:220)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:89)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:843)
这些是我的 BomItem 类的属性:
These are the attributes of my BomItem class:
private int itemId;
private Collection<BomModule> modules;
private boolean deprecated;
private String partNumber;
private String description; //LOB
private int quantity;
private String unitPriceDollar;
private String unitPriceEuro;
private String discount;
private String totalDollar;
private String totalEuro;
private String itemClass;
private String itemType;
private String vendor;
private Calendar listPriceDate;
private String unitWeight;
private String unitAveragePower;
private String unitMaxHeatDissipation;
private String unitRackSpace;
我引用的BomModule 类的属性:
private int moduleId;
private String moduleName;
private boolean isRootModule;
private Collection<BomModule> parentModules;
private Collection<BomModule> subModules;
private Collection<BomItem> items;
private int quantity;
知道是什么导致了这个错误吗?我该如何解决?
Any idea what causes this error? How can I fix it?
推荐答案
那个问题是你有一个循环引用.
That problem is that you have a circular reference.
在您引用的 BomModule
类中:
private Collection<BomModule> parentModules;
private Collection<BomModule> subModules;
对BomModule
的自引用,显然GSON根本不喜欢.
That self reference to BomModule
, obviously, not liked by GSON at all.
解决方法是将模块设置为 null
以避免递归循环.这样我就可以避免 StackOverFlow-Exception.
A workaround is just set the modules to null
to avoid the recursive looping. This way I can avoid the StackOverFlow-Exception.
item.setModules(null);
或者使用transient
关键字标记您不希望在序列化json中显示的字段,例如:
Or mark the fields you don't want to show up in the serialized json by using the transient
keyword, eg:
private transient Collection<BomModule> parentModules;
private transient Collection<BomModule> subModules;
这篇关于gson.toJson() 抛出 StackOverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!