java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:818)
........................................................................................................................
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2170)
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1332)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:749)
... 51 more
Caused by: java.lang.IllegalArgumentException
at java.sql.Date.getHours(Date.java:182)

... 61 more

今天在写代码的时候,突然出现这个问题,找了好久的错误,最后终于找到了,原来是因为bean里有Date字段,且从数据库里读出来的是java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错,

解决方法一:

public Date getPdate() {
        return new java.util.Date(pdate.getTime());
        
    }

把Bean文件中,date类型的数据封装一下,就没问题了

解决方法二:

也可以把这个Date类型的数据,不使用Date类型而使用String类型。

猜你喜欢

转载自www.cnblogs.com/2019wxw/p/10989814.html