首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

There is a cycle in the hierarchy解决方法

2012-07-03 
There is a cycle in the hierarchy解决办法http://www.diybl.com/course/1_web/javascript/jsjs/20090415

There is a cycle in the hierarchy解决办法
http://www.diybl.com/course/1_web/javascript/jsjs/20090415/165099.html

property 'delegate' has no getter method异常参考:http://panyongzheng.iteye.com/blog/1492278

解决办法

1.设置JSON-LIB让其过滤掉引起循环的字段。这个比较能解决问题,第2种方式只排除一个类,但是Hibernate会用到很多类,很难排除所有。

JsonConfig config = new JsonConfig();          config.setIgnoreDefaultExcludes(false);          config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);          config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));           config.setExcludes(new String[] {// 只要设置这个数组,指定过滤哪些字段。          "ctuAcademicStaffCcs",           "ctuProjects"});          System.out.println(JSONSerializer.toJSON(list, config).toString());


2.设置JSON-LIB的setCycleDetectionStrategy属性让其自己处理循环,省事但是数据过于复杂的话会引起数据溢出或者效率低下。
JsonConfig config = new JsonConfig();config.setIgnoreDefaultExcludes(false);config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd")); // date processor// registerString tempStr = "{"TotalRecords":" + total.toString() + ","Datas":" + JSONSerializer.toJSON(list, config).toString() + "}";out.print(tempStr);


--------------------
一下是用到的三个类:
package com.common;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import net.sf.json.JsonConfig;import net.sf.json.processors.JsonValueProcessor;public class DateJsonValueProcessor implements JsonValueProcessor {public static final String Default_DATE_PATTERN = "yyyy-MM-dd";private DateFormat dateFormat;public DateJsonValueProcessor(String datePattern) {try {dateFormat = new SimpleDateFormat(datePattern);} catch (Exception e) {dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);}}public Object processArrayValue(Object value, JsonConfig jsonConfig) {return process(value);}public Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {return process(value);}private Object process(Object value) {return dateFormat.format((Date) value);}}



package com.common;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import net.sf.json.processors.*;public class HibernateJsonBeanProcessor implements JsonBeanProcessor {public JSONObject processBean(Object obj, JsonConfig jsonConfig) {return new JSONObject();}}



package com.common;import net.sf.json.processors.*;    import java.util.Set;    import org.apache.log4j.Logger;        public class HibernateJsonBeanProcessorMatcher extends JsonBeanProcessorMatcher {        private static Logger log = Logger            .getLogger(HibernateJsonBeanProcessorMatcher.class);        @Overridepublic Object getMatch(Class target, Set set) {System.out.println("@target.getName()="+target.getName());if (target.getName().contains("$$EnhancerByCGLIB$$")) {log.warn("Found Lazy-References in Hibernate object " + target.getName());return org.hibernate.proxy.HibernateProxy.class;}return DEFAULT.getMatch(target, set);}  } 

热点排行