java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean

我的未来我决定 提交于 2019-11-27 07:21:13

一、第一眼看到net.sf.ezmorph.bean.MorphDynaBean这个异常,觉得好生僻。不禁感慨这是什么幺蛾子。后来查了下,简单解释下:是由于需要转换的对象中存在其他对象的引用,并且在转换的时候没有指定对应的类,此时json会使用默认的bean进行动态转换MorphDynaBean,但是在使用的时候就会出现类型转换异常。

二、场景重现
假设有这样一段代码

net.sf.json.JSONArray jsonarray = net.sf.json.JSONArray.fromObject(param.getBody());
List<OuyeelCommissionConfirm> resultList=(List<OuyeelCommissionConfirm>)net.sf.json.JSONArray.toCollection(jsonarray,OuyeelCommissionConfirm.class);
if(resultList!=null){
	for(OuyeelCommissionConfirm o:resultList){
		List<ImageModel> imageList=o.getImageList();
		for(ImageModel m:imageList){//标识为特别行
		}
	}
}

OuyeelCommissionConfirm.java如下,里面有两个list类型的成员变量

/**
 * 签收单信息对象
 * @author shixiangcheng
 * 2019-08-13
 */
public class OuyeelCommissionConfirm implements Serializable{
	private static final long serialVersionUID = 1L;
	private String commissionId; //运帮委托单号
	private String sumReturnWeight; //总计返单量
	private String sumOtherAmount; //总计杂费金额
	private String status; //0:撤销返单1:提交返单
	private List<ImageModel> imageList;//图片
	private List<ConfirmReceiverItem> confirmItemList;

通过客户端HttpClient调用报错,报错的是第一段代码标识为特别行的内容

java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean

三,解决方案
第一段代码做如下修改

net.sf.json.JSONArray jsonarray = net.sf.json.JSONArray.fromObject(param.getBody());
			
Map<String,Object> classMap=new HashMap<String,Object>();
classMap.put("confirmItemList", ConfirmReceiverItem.class);
classMap.put("imageList", ImageModel.class);
classMap.put("confirmDetailList", ConfirmDetailModel.class);
OuyeelCommissionConfirm [] arrys=(OuyeelCommissionConfirm [])JSONArray.toArray(jsonarray,OuyeelCommissionConfirm.class,classMap);
List<OuyeelCommissionConfirm> resultList=Arrays.asList(arrys);
			
if(resultList!=null){
	for(OuyeelCommissionConfirm o:resultList){
		List<ImageModel> imageList=o.getImageList();
		for(ImageModel m:imageList){
		}
	}
}

问题解决

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!