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());
  • 相关阅读:
    SQL常用优化手法
    winform 文件上传
    在主线程中开启同步任务,为什么会阻塞线程
    KVC与KVO
    android shape的使用
    如何导入开源工程
    图片资源的设定
    如何进行程序的反编译和防止反编译
    log的管理
    通过反射机制拿到被隐藏的类(获取应用程序的缓存大小)
  • 原文地址:https://www.cnblogs.com/jyluo03/p/2574295.html
Copyright © 2011-2022 走看看