从com.google.gson.Gson.toJson获取StackOverflowError(对象arg0,类型arg1,可附加arg2)

时间:2013-10-08 23:26:09

标签: java javascript gson

知道为什么我从以下代码中收到此错误? 。 。 ..

PrintWriter writer = response.getWriter();
Collection<Alert> alerts = AppState.instance.bi.getBackend().getNewerAlerts(time);
System.out.println("Alerts size = " + alerts.size());  
Gson gson = gsonBuilder.create();
gson.toJson(new AlertsResponse(alerts), AlertsResponse.class, writer);
  

线程中的异常&#34; http-bio-80-exec-11&#34; java.lang.StackOverflowError的         at java.util.LinkedHashMap $ LinkedHashIterator。(Unknown Source)         at java.util.LinkedHashMap $ LinkedHashIterator。(Unknown Source)         at java.util.LinkedHashMap $ ValueIterator。(Unknown Source)         at java.util.LinkedHashMap $ ValueIterator。(Unknown Source)         at java.util.LinkedHashMap.newValueIterator(Unknown Source)         at java.util.HashMap $ Values.iterator(Unknown Source)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:192)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)         在com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:105)         在com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1.write(ReflectiveTypeAdapterFactory.java:89)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.write(ReflectiveTypeAdapterFactory.java:195)

1 个答案:

答案 0 :(得分:5)

AlertsResponse类层次结构中的一个字段导致循环引用,导致堆栈溢出。

识别导致此问题的字段,并在字段前使用@Expose(serialize = false)注释更改结构或阻止序列化,例如

@Expose(serialize = false) public SelfReferencingObject notMe;