gson.toJson() throws StackOverflowError
That problem is that you have a circular reference.
In the BomModule
class you are referencing to:
private Collection<BomModule> parentModules;
private Collection<BomModule> subModules;
That self reference to BomModule
, obviously, not liked by GSON at all.
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);
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;
I had this problem when I had a Log4J logger as a class property, such as:
private Logger logger = Logger.getLogger(Foo.class);
This can be solved by either making the logger static
or simply by moving it into the actual function(s).