Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson StackOverflowError

Tags:

java

json

gson

I have a simple return class that either takes an exception or one of our business objects. If the REST web service method throws an exception, it catches it and sets the exception in the response. If not, it sets the return value in the response and serializes it.

In my case, I'm getting an InvocationTargetError that wraps one of our service exceptions. I set this on my exception on my return class and get the following stack trace:

java.lang.StackOverflowError
com.google.gson.reflect.TypeToken.hashCode(TypeToken.java:280)
java.util.HashMap.get(HashMap.java:300)
java.util.Collections$SynchronizedMap.get(Collections.java:1975)
com.google.gson.Gson.getAdapter(Gson.java:337)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
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.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)
and so on...

Anyone have any ideas? This looks like a bug in Gson 2.2.2.

like image 632
Thom Avatar asked Jan 14 '23 23:01

Thom


1 Answers

Gson can't handle circular references in the serialized data. Chances are you have one. Fix that and you've fixed your problem.

like image 155
Jesse Wilson Avatar answered Jan 21 '23 11:01

Jesse Wilson