net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案

Posted silentmuh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案相关的知识,希望对你有一定的参考价值。

原因分析:在解析bean时,出现死循环调用,即:多个Bean之间出现了相互调用
解决办法:过滤去掉bean中引起死循环调用的属性:

[java] view plain copy
 
    1. List<Project> projectList = projectServices.find();  //获取数据  
    2.  //自定义JsonConfig用于过滤Hibernate配置文件所产生的递归数据  
    3.  JsonConfig config = new JsonConfig();  
    4.  config.setExcludes(new String[]{"documentSet","milestoneSet","issuesSet","userSet"});  //只要设置这个数组,指定过滤哪些字段。     
    5.  //组成JSON数组  
    6.  JSONArray json = JSONArray.fromObject(projectList, config);  

 

 

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

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

 

技术分享图片
 1 //得到所有部门
 2     //返回json对象字符串
 3     public String getAllDep(){
 4         List list = deptDAO.findAll();
 5         JsonConfig config = new JsonConfig();
 6         config.setExcludes(new String[]{"emps"});//除去emps属性
 7         String json = JSONArray.fromObject(list, config).toString();
 8         return json;
 9     }
10     
11     //得到所有员工
12     public String getAllEmp(int id){
13         List list = empDAO.findByProperty("dept.deptId", id);
14         JsonConfig config = new JsonConfig();
15         config.setExcludes(new String[]{"dept"});//除去dept属性
16         String json = JSONArray.fromObject(list, config).toString();
17         return json;
18     }
技术分享图片

 

 

异常代码如下:

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

 


以上是关于net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案的主要内容,如果未能解决你的问题,请参考以下文章

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

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetExce

json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

json格式错误:net.sf.json.JSONException: A JSONObject text must begin with '' at character 1 of 2

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

net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案