net.sf.json.JSONException-java.lang.reflect.InvocationTargetException-java.lang.NullPointerExcept
目录
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException & java.lang.NullPointerExcept
严重: Servlet.service() for servlet [centralServlet] in context with path [/UserTest] threw exception [Request processing failed; nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause
java.lang.NullPointerException
at org.hibernate.tuple.entity.EntityMetamodel.isNaturalIdentifierInsertGenerated(EntityMetamodel.java:1127)
上面的异常是在执行下面的语句出现的:
JSONArray array = JSONArray.fromObject(this.users);
出现这bug
出现了两种情况:
1.日期格式
2.hibernate延时加载
解决:日期格式
Java code
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.registerJsonBeanProcessor(java.sql.Date.class,new JsDateJsonBeanProcessor());
或者
private java.util.Date createTime;
只在字段前声明Date的数据类型可能也会抛异常,在Set,get方法中,有出现Date类型的都把包名加上
解决:hibernate延时加载 设置
Java code
JsonConfig cfg = new JsonConfig();
cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
还有一个办法就是建一个 model 类,将需要的数据取出来放到 model 对象中,然后把 model 对象转换成 json 对象