zoukankan      html  css  js  c++  java
  • 【转】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());
  • 相关阅读:
    Network In Network
    UNIX环境高级编程之第4章:文件和文件夹-习题
    SqlServer 错误日志切换和查看
    hihoCoder
    Mono源码学习笔记:Console类(四)
    Java transientkeyword使用小记
    高速排序算法具体解释与实现
    The Triangle
    iOS开发实践之GET和POST请求
    C#重构经典全面汇总
  • 原文地址:https://www.cnblogs.com/jyluo03/p/2574295.html
Copyright © 2011-2022 走看看