为了账号安全,请及时绑定邮箱和手机立即绑定

使用gson.toJson(object)时发生错误:java.lang.StackOverflowError,求解决办法?

使用gson.toJson(object)时发生错误:java.lang.StackOverflowError,求解决办法?

红糖糍粑 2019-04-19 15:59:32
strut2+hibernate+spring.现有有2个对象:shippingList(物流公司的托运单)和receiver(收货人),这2个对象设置了双向级联关系以下是这2个对象的实体类代码:@Entity(name="t_shippingList")publicclassFreightCenter_shippingListextendsSystemOther_entityModel{privateIntegerindexs;//主键,唯一标志privateStringid;//托运单号privateReferenceRoom_receiverreceiver;//收货人............@ManyToOne(cascade={CascadeType.MERGE},fetch=FetchType.EAGER)@JoinColumn(name="receiver_indexs")publicReferenceRoom_receivergetReceiver(){returnreceiver;}publicvoidsetReceiver(ReferenceRoom_receiverreceiver){this.receiver=receiver;}}@Entity(name="t_receiver")publicclassReferenceRoom_receiverextendsSystemOther_entityModel{privateIntegerindexs;//主键privateStringid;//客户代号..................privateSetshippingList;//托运单..................@OneToMany(fetch=FetchType.LAZY,mappedBy="receiver")publicSetgetShippingList(){returnshippingList;}publicvoidsetShippingList(SetshippingList){this.shippingList=shippingList;}}我们希望可以通过shippingList这个对象能够查询到receiver这个对象,添加修改shippingList的时候,也能添加修改receiver这个对象.通过receiver这个对象也能查询shippingList这个对象.对查询出来的数据,我们使用gson将它转换成json格式的数据,但是,当我们使用gson.toJson(object)语句进行转换时,发生了错误。错误代码如下:Stacktracesjava.lang.reflect.InvocationTargetExceptionsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:601)com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Thread.java:722)java.lang.StackOverflowErrorjava.lang.StringBuffer.append(StringBuffer.java:237)java.io.StringWriter.write(StringWriter.java:101)com.google.gson.stream.JsonWriter.string(JsonWriter.java:534)com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)com.google.gson.stream.JsonWriter.value(JsonWriter.java:495)com.google.gson.internal.bind.TypeAdapters$7.write(TypeAdapters.java:246)com.google.gson.inter2000nal.bind.TypeAdapters$7.write(TypeAdapters.java:231)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.Gson$FutureTypeAdapter.write(Gson.java:892)com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)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.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)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.Gson$FutureTypeAdapter.write(Gson.java:892)com.google.gb20son.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)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.Gson$FutureTypeAdapter.write(Gson.java:892)com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)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.Gson$FutureTypeAdapter.write(Gson.java:892)com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)我想,之所以发生java.lang.StackOverflowError这个错误,是因为receiver(收货人)这个对象是shippingList(托运单)这个对象的一个属性,同时,shippingList(托运单)也是receiver(收货人)的一个属性,数据关联进入了死循环导致了这个问题.可是另外一方面,我觉得很hibernate会解决双向级联后引起的这个问题。我的问题是:1)我的级联设置中是不是出现了什么问题?2)对于这个错误,我该如何解决?
查看完整描述

2 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

之所以出现这个错误,是因为双向关联时相互引用,导致进入死循环,解决办法是gson转换成json时,忽略receiver.shippingList这个字段,这个字段不用转换就可以了.
                            
查看完整回答
反对 回复 2019-04-19
?
慕容3067478

TA贡献1773条经验 获得超3个赞

对查询出来的数据,我们使用gson将它转换成json格式的数据,但是,当我们使用gson.toJson(object)语句进行转换时,发生了错误。
                            
查看完整回答
反对 回复 2019-04-19
  • 2 回答
  • 0 关注
  • 386 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信