4
votes

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.

2
Did you make sure there are no circular references? stackoverflow.com/questions/10209959/… (That' what had happend to me) - MartinK
It's an exception. What would be circular? - Thom
Scratching my head - Turns out Throwable's cause field is the problem. See groups.google.com/forum/?fromgroups=#!topic/google-gson/… - MartinK

2 Answers

6
votes

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

3
votes

I created an issue for this on the google code site.

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