Gson StackOverflowError

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

In my case, I get an InvocationTargetError that wraps one of our utility exceptions. I installed this in my exception from my return class and got 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... 

Does anyone have any ideas? This seems like a bug in Gson 2.2.2.

+4
source share
2 answers

I created a problem for this on a Google code site.

http://code.google.com/p/google-gson/issues/detail?id=488

+3
source

Gson cannot handle circular references in serialized data. Most likely you have it. Correct it and you have fixed your problem.

+5
source

Source: https://habr.com/ru/post/1447265/


All Articles