net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
json-lib对象转化为json数据抛net.sf.json.JSONException: va.lang.reflect.InvocationTargetException异常
出现了两种情况:
- 日期格式
- hibernate延时加载
解决:日期格式
private java.util.Date createTime;
只在字段前声明Date的数据类型可能也会抛异常,在Set,get方法中,有出现Date类型的都把包名加上
解决:hibernate延时加载 设置
- JsonConfigcfg=newJsonConfig();
- cfg.setExcludes(newString[]{"handler","hibernateLazyInitializer"});
方法举例
- @Transactional(readOnly=true)
- publicJSONArraydatagrid(Pager<AddressBook>page,Useruser,DetachedCriteriadetachedCriteria){
- JsonConfigcfg=newJsonConfig();
- cfg.setJsonPropertyFilter(newPropertyFilter()
- {
- publicbooleanapply(Objectsource,Stringname,Objectvalue){
- if(name.equals("addressGroup")||name.equals("user")||name.equals("createTime")||name.equals("birthday")){
- returntrue;
- }else{
- returnfalse;
- }
- }
- });
- cfg.setExcludes(newString[]{"handler","hibernateLazyInitializer"});
- Pager<AddressBook>pager=this.findAllByApprove(page,user,detachedCriteria);
- longtotal=pager.getTotalCount();
- List<AddressBook>list=pager.getResult();
- Map<String,Object>result=newHashMap<String,Object>();
- result.put("total",total);
- result.put("rows",list);
- JSONArrayjsonArray=JSONArray.fromObject(result,cfg);
- returnjsonArray;
- }