net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
今天在用List集合转换成json数组的时候发生了这个错误,这个错误的意思就是说:我要转换成json数组的对象中含有对象嵌套引用。
使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系。比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常。
解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面:
//得到所有部门
//返回json对象字符串
public String getAllDep(){
List list = deptDAO.findAll();
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"emps"});//除去emps属性
String json = JSONArray.fromObject(list, config).toString();
return json;
} //得到所有员工
public String getAllEmp(int id){
List list = empDAO.findByProperty("dept.deptId", id);
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"dept"});//除去dept属性
String json = JSONArray.fromObject(list, config).toString();
return json;
}
异常代码如下:
严重: 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)
来自:http://www.cnblogs.com/liuling/archive/2013/02/07/dasfdf.html
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案的更多相关文章
- json-lib-2.4-jdk15.jar 报错 net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案(Hibernate)
使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决 ...
- atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy 1. 环境:使用hibernate4跟个,,要不个哪的对象系列 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!
因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法
使用Hibernate manytoone属性关联主表的时候,如果使用JSONArray把pojo对象转换成json对象时,很容易出现循环的异常.解决的办法就是, 在转换json对象时忽略manyto ...
- net.sf.json.JSONException: There is a cycle in the hierarchy! 转json死循环问题解决
解决上述问题遵照两个原则就可以: 1.页面不需要展示关联数据时 解决:将关联对象属性排除掉 2.页面需要展示关联数据时 解决:将关联对象改为立即加载,并且将关联对象中的属性排除
- Json_异常_net.sf.json.JSONException: JSONObject["solution"] not found.
net.sf.json.JSONException: JSONObject["solution"] not found. 没有这个元素造成的. 问题代码: JSONObject j ...
- net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- XML2JSON 的【net.sf.json.JSONException: nu.xom.ParsingException must be followed by either attribute specifications, ">" or "/>"】问题解决办法
在使用JSon-Lib库进行XML2JSon的转换时,在JUnit测试时没有什么问题,但是在Tomcat里面跑的时候,抛出了下面的异常,查找了google,发现关于这方便的文章比较少,即使有,也需要F ...
随机推荐
- thinkPHP判断是否修改成功
thinkPHP中使用save方法来更新数据的save方法的正常执行时返回值是影响的记录数,出错时返回false,返回为0和返回false在很多业务场景下都是不同的. 而当修改的内容和原有内容一致的时 ...
- Ubuntu安装完之后需要做的事情
字体推荐思源 lantern可以设置全局代理 安装好了ubuntu之后,安装gnome主题 安装Gnome之前,升级系统: $ sudo apt update $ sudo apt upgrade 1 ...
- spring in action 学习笔记五:@Autowired这个注解如何理解
@Autowired这个注解的意思就是自动装配.他把一个bean对象自动装配到另一个对象中.下面的案例证明了spring的自动装配. 定义一个Sixi类.代码如下: package com.qls.a ...
- Broadcom GNSS xxx Geolocaltion Sensor与Windows导航程序的兼容性(转)
Broadcom是Windows 8(3G)平板普遍采用的一款GPS传感器, 其windows驱动程序可以提供GNSS接口.GNSS接口提供的数据,说实话确实比普通手机的数据好.在开机.室外.无AGP ...
- 花匠(NOIP2013)(神奇纯模拟)
原题传送门 这是道很奇怪的题目,真不知道为什么要放到T2. 也许是T1太水了 首先先看题, 题目要求一个数列中下标为偶数的点比临近的下表为奇数的点更大或更小 其实就是说在原数组中找到一个最长的波动数列 ...
- switch与if 性能测试
测试结果:switch性能更高. 测试过程:新建一个Win32 Console Application, 在cpp文件中添加下面代码 #include "stdafx.h" #in ...
- MFC中使用sqlite3操作数据库
需要用到的文件有sqlite3.h .sqlite3.dll.sqlite3.lib.网上很多人分享下载地址这里不再赘述. 将这三个文件拷贝到自己新建MFC项目目录下,在解决方案窗口下 添加现有项,选 ...
- 谈谈dpdk应用层包处理程序的多进程和多线程模型选择时的若干考虑
看到知乎上有个关于linux多进程.多线程的讨论:http://www.zhihu.com/question/19903801/answer/14842584 自己项目里也对这个问题有过很多探讨和测试 ...
- Appium+python自动化4-元素定位uiautomatorviewer【转载】
前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...
- js面试总结
<div id="app"> <button onClick="app()">点击1</button> <button ...