【转】net.sf.json.JSONException: java.lang.reflect.InvocationTargetException


今天在项目中使用了

 

 1 PageGridUtil util = new PageGridUtil(request);
 2 
 3 JsonConfig config = new JsonConfigBuilder().createJsonConfig(new String[] {});
 4 
 5 JSONObject summaryJson = JSONObject.fromObject(vo);
 6 
 7 JSONObject pageObj = new JSONObject();
 8 
 9 pageObj.put("data1", summaryJson);
10 
11 util.renderJSON(response, pageObj.toString());

 

来封装JSON对象

 

 

在封装对象JSONObject.fromObject(vo)时发生了一下异常:

1 net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 
2     net.sf.json.JSONObject._fromBean(JSONObject.java:959)  

网上查了一下,发现是:在hibernate配置文件中用到了:

<property name="shenqingriqi"type="date"></property>

可能问题一:

    JSON对象把date认为是java.sql.Date

解决方案:

hibernate配置文件改为:

<property name="shenqingriqi"type="java.util.Date"></property>

这样应该就可以了。

 

可能问题二:

    在封装对象时有可能hibernate级联到了别的对象,使用到了延迟加载而导致了这个问题

解决方案:

 1 PageGridUtil util = new PageGridUtil(request);
 2 
 3 JsonConfig config = new JsonConfigBuilder().createJsonConfig(new String[] {"handler","hibernateLazyInitializer"});
 4 
 5 JSONObject summaryJson = JSONObject.fromObject(vo, config);
 6 
 7 JSONObject pageObj = new JSONObject();
 8 
 9 pageObj.put("data1", summaryJson);
10 
11 util.renderJSON(response, pageObj.toString());

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM