资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

net.sf.json.JSONObject类型转换报错-net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.xxx.xxx

Java 更新时间: 发布时间: 计算机考试归档 最新发布

net.sf.json.JSONObject类型转换报错-net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.xxx.xxx

一、场景

1、将net.sf.json.JSONObject转为实体对象

2、实体对象包含List类型的属性

3、操作List属性时报错(本篇以遍历List时报错为例)

二、报错信息

java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.xxx.entity.serviceflow.BranchNodeCondition	at com.xxx.util.ServiceFlowHandlerUtil.matchBranchHandle(ServiceFlowHandlerUtil.java:519)	at org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService$ConsumeRequest.run(ConsumeMessageConcurrentlyService.java:411)	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)	at java.util.concurrent.FutureTask.run(FutureTask.java:266)	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)	at java.lang.Thread.run(Thread.java:748)

三、代码

1、实体

package com.xxx.entity.serviceflow;import lombok.Data;import java.util.List;@Datapublic class BranchNodeData {    private Long branchId;    private Integer seq;    private String branchType;    private String branchMemo;    private Long branchJumpId;	//实体包含List类型的属性    private List conditionList;}

2、测试

package com.xxx;import com.xxx.entity.serviceflow.BranchNodeCondition;import com.xxx.entity.serviceflow.BranchNodeData;import net.sf.json.JSONObject;import java.util.ArrayList;import java.util.List;public class Test {    public static void main(String[] args) {        //构建对象        BranchNodeData data = new BranchNodeData();        data.setBranchId(1L);        data.setBranchJumpId(10L);        data.setBranchMemo("默认");        data.setBranchType("1");        data.setSeq(0);        //设置conditionList属性        List conditionList = new ArrayList<>();        BranchNodeCondition condition = new BranchNodeCondition();        condition.setId(1L);        condition.setJudgeType("0");        condition.setParamGroup(1);        condition.setParamName("name");        condition.setParamValue("盖伦 与 卡特琳娜");        condition.setPgJudgeType("0");        conditionList.add(condition);        data.setConditionList(conditionList);        JSONObject json = JSONObject.fromObject(data);        System.out.println("BranchNodeData转为json字符串:n" + json);        System.out.println("n-------------华丽的分割线-------------n");		//------以下代码为重点,前面的代码只是铺垫------        BranchNodeData o = (BranchNodeData) JSONObject.toBean(json, BranchNodeData.class);        System.out.println("json字符串转为BranchNodeData:n" + o.toString() + "n");                //在遍历conditionList属性时报错        for (BranchNodeCondition info : o.getConditionList()) {            System.out.println("condition信息为:n" + info);        }    }}

四、原因

1、JsonObject 对象强转 Java对象,且JsonObject对象中含有数组类型(ArrayList)的属性时出现

2、在转换时不会报错,但是想要拿到转换后的数组时会报这个错误

3、以本篇代码为例,可以看到强转后的conditionList属性类型与原先的属性类型是不一致的(原先类型为:BranchNodeCondition,转换后却是:net.sf.ezmorph.bean.MorphDynaBean)

BranchNodeData转为json字符串:{"branchId":1,"branchJumpId":10,"branchMemo":"默认","branchType":"1","conditionList":[{"id":1,"judgeType":"0","paramGroup":1,"paramName":"name","paramValue":"盖伦 与 卡特琳娜","pgJudgeType":"0"}],"seq":0}-------------华丽的分割线-------------json字符串转为BranchNodeData:BranchNodeData(branchId=1, seq=0, branchType=1, branchMemo=默认, branchJumpId=10, conditionList=[net.sf.ezmorph.bean.MorphDynaBean@396e2f39[  {paramGroup=1, judgeType=0, id=1, paramName=name, paramValue=盖伦 与 卡特琳娜, pgJudgeType=0}]])

五、解决

JsonObject中含有List属性,所以需要用map映射一下

以本篇为例

		//注释掉原先转换的代码		//BranchNodeData o = (BranchNodeData) JSONObject.toBean(json, BranchNodeData.class);				//定义Map映射        Map> classMap = new HashMap<>();        //Map-Key为实体对象List属性的属性名称        //Map-Value为实体对象List属性的属性类型        classMap.put("conditionList", BranchNodeCondition.class);        //指定Map映射        BranchNodeData o = (BranchNodeData) JSONObject.toBean(json, BranchNodeData.class, classMap);
转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/1095019.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【net.sf.json.JSONObject类型转换报错-net.sf.ezmorph.bean.MorphDynaBean cannot be cast to com.xxx.xxx】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2