• 多种方式解决Java控制台报错 java.util.LinkedHashMap cannot be cast to.....


    问题描述

    今天在使用RestTemplate调用服务的时候,因为服务提供者返回的是一个List集合,所以在使用消费者调用的时候,restTemplate.getForObject()期待返回的类型直接写成了List.class

    相关代码如下

     public List<DocInfoRela> getFilePath() {
            String url = serviceUrl + "/common/getFilePath";
            List<DocInfoRela> docInfoRelas = restTemplate.getForObject(url, List.class);
            return docInfoRelas;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    生产者代码
    在这里插入图片描述
    消费者代码

    在这里插入图片描述

    但是接收到List之后,在便利的时候却报错了,报错内容如下:

    java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to cn.chinatowercom.postaccounting.entity.DocInfoRela

    ERROR 2022-11-17 15:09:32 [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to cn.chinatowercom.postaccounting.entity.DocInfoRela] with root cause

    相关代码和如图截图:
    在这里插入图片描述

    解决问题

    方式1

    我个人比较喜欢这种方案

    定义返回List
    在这里插入图片描述

        public  List<DocInfoRela> getFilePath() {
            String url = serviceUrl + "/common/getFilePath";
            List<DocInfoRela> docInfoRelas = restTemplate.getForObject(url, List.class);
            return docInfoRelas;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后将list再次转为json串,然后由json串再转为list
    在这里插入图片描述
    代码如下

     		// 从数据库获取到数据
                List<DocInfoRela> DocInfoRelasList = this.getFilePath();
                // 将list再次转为json串,然后由json串再转为list
                String docInfoRelaStrings = JSON.toJSONString(DocInfoRelasList);
                DocInfoRelasList = JSON.parseArray(docInfoRelaStrings, DocInfoRela.class);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    方式2

    返回结果以String类型接受,也即接受的是一个json字符串
    在这里插入图片描述
    代码如下

     public String getFilePath() {
            String url = serviceUrl + "/common/getFilePath";
            String docInfoRelas = restTemplate.getForObject(url, String.class);
            return docInfoRelas;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后再使用阿里巴巴的fastjson将json字符串转变成list集合
    在这里插入图片描述
    代码如下

    			 // 从数据库获取到文件路径
                String jsonString = this.getFilePath();
                // 将json字符串转集合
                List<DocInfoRela> docInfoRelas = JSON.parseArray(jsonString, DocInfoRela.class);
    
    • 1
    • 2
    • 3
    • 4

    方式3

    备注:这个我没有试过,在网上找到的一种方案

    首先导入net.sf.json 类

        <dependency>
           <groupId>net.sf.json-lib</groupId>
           <artifactId>json-lib</artifactId>
           <version>2.3</version>
           <classifier>jdk15</classifier>
         </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    然后 使用JSONObject中的方法, 先将数据转成json字符串, 在转成实体对象即可。

    JSONObject jsonObject=JSONObject.fromObject(objectStr); // 将数据转成json字符串
    Person per = (DocInfoRela)JSONObject.toBean(jsonObject, DocInfoRela.class); //将json转成需要的对象
    
    • 1
    • 2

    总结

    以上就是遇到java.util.LinkedHashMap cannot be cast to…的几种解决思路,我个人喜欢第一种,如有什么问题,欢迎讨论留言

  • 相关阅读:
    MATLAB算法实战应用案例精讲-【图像处理】SLAM技术详解(补充篇)
    04.4. 模型选择、欠拟合和过拟合
    KS001 基于Springboot机票预订系统
    大数据定价方法的国内外研究综述及对比分析
    Java基本数据类型
    Clear Code for Minimal API
    使用设计模式基于easypoi优雅的设计通用excel导入功能
    K8S日志收集
    基于划分的聚类分析——K-means(机器学习)
    restful
  • 原文地址:https://blog.csdn.net/weixin_46713508/article/details/127904496