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

來源:互聯網
上載者:User

標籤:des   style   http   ar   io   color   os   使用   sp   

使用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 exceptionnet.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.