【轉】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