• 多种方式解决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…的几种解决思路,我个人喜欢第一种,如有什么问题,欢迎讨论留言

  • 相关阅读:
    全排列与组合
    Windows内核对象
    MFC使用NuGet安装库并使用库 比如tinyxml
    vcpkgC++开源项目1
    电源管理芯片代理商:电源管理芯片的领域和封装
    21个新的ChatGPT应用
    小白也能搞通UDP通信(88E1111 RGMII 接口)
    Centos7常用服务脚本(.service)
    C++生化危机1.5源码
    用Airtest快速实现手机文件读写与删除功能
  • 原文地址:https://blog.csdn.net/weixin_46713508/article/details/127904496