net.sf.json.JSONException: There is a cycle in the

前端之家收集整理的这篇文章主要介绍了net.sf.json.JSONException: There is a cycle in the前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系。比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常。

  解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面:

//得到所有部门
//返回json对象字符串
publicStringgetAllDep(){
Listlist=deptDAO.findAll();
JsonConfigconfig=newJsonConfig();
config.setExcludes(newString[]{"emps"});//除去emps属性
Stringjson=JSONArray.fromObject(list,config).toString();
returnjson;
}

//得到所有员工
publicStringgetAllEmp(intid){
Listlist=empDAO.findByProperty("dept.deptId",id);
JsonConfigconfig=newJsonConfig();
config.setExcludes(newString[]{"dept"});//除去dept属性
Stringjson=JSONArray.fromObject(list,config).toString();
returnjson;
}

异常代码如下:

严重:Servlet.service()forservletspringMVCthrewexception
net.sf.json.JSONException:Thereisacycleinthehierarchy!
atnet.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
atnet.sf.json.JSONObject._fromBean(JSONObject.java:857)
atnet.sf.json.JSONObject.fromObject(JSONObject.java:192)
atnet.sf.json.JSONObject._processValue(JSONObject.java:2774)
atnet.sf.json.JSONObject._setInternal(JSONObject.java:2798)
atnet.sf.json.JSONObject.setValue(JSONObject.java:1507)
atnet.sf.json.JSONObject._fromBean(JSONObject.java:940)
atnet.sf.json.JSONObject.fromObject(JSONObject.java:192)
atnet.sf.json.JSONArray._processValue(JSONArray.java:2557)
atnet.sf.json.JSONArray.processValue(JSONArray.java:2588)
atnet.sf.json.JSONArray.addValue(JSONArray.java:2575)
atnet.sf.json.JSONArray._fromCollection(JSONArray.java:1082)
atnet.sf.json.JSONArray.fromObject(JSONArray.java:145)
atnet.sf.json.JSONObject._processValue(JSONObject.java:2749)
atnet.sf.json.JSONObject._setInternal(JSONObject.java:2798)
atnet.sf.json.JSONObject.setValue(JSONObject.java:1507)
atnet.sf.json.JSONObject._fromBean(JSONObject.java:940)
atnet.sf.json.JSONObject.fromObject(JSONObject.java:192)
atnet.sf.json.JSONArray._processValue(JSONArray.java:2557)
atnet.sf.json.JSONArray.processValue(JSONArray.java:2588)
atnet.sf.json.JSONArray.addValue(JSONArray.java:2575)
atnet.sf.json.JSONArray._fromCollection(JSONArray.java:1082)
atnet.sf.json.JSONArray.fromObject(JSONArray.java:145)
atcom.service.EmpService.getAllDep(EmpService.java:31)
atcom.service.EmpService$$FastClassByCGLIB$$fef4bb53.invoke(<generated>)
atnet.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
atorg.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)
atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
atorg.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
atorg.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
atorg.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
atcom.service.EmpService$$EnhancerByCGLIB$$8fce1d77.getAllDep(<generated>)
atcom.action.DepAction.getAllDept(DepAction.java:22)
atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
atjava.lang.reflect.Method.invoke(Method.java:601)
atorg.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:413)
atorg.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:134)
atorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:310)
atorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:297)
atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
atorg.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:690)
atjavax.servlet.http.HttpServlet.service(HttpServlet.java:803)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
atorg.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
atjava.lang.Thread.run(Thread.java:722)
原文链接:https://www.f2er.com/json/289948.html

猜你在找的Json相关文章