简体   繁体   English

class java.util.LinkedHashMap 不能转换为 class [...]

[英]class java.util.LinkedHashMap cannot be cast to class [...]

I'm making a GET request to a WooCommerce API to fetch orders and my idea is to grab some data from the response and insert it into a local database.我正在向 WooCommerce API 发出 GET 请求以获取订单,我的想法是从响应中获取一些数据并将其插入本地数据库。 I tested the request and everything works correctly.我测试了请求,一切正常。 So I continued using QuickType to be able to create the classes corresponding to the response, so I have a way to save it and insert it into the database.所以我继续使用 QuickType 能够创建对应于响应的类,所以我有办法将它保存并插入到数据库中。 The problem is the following, if I comment the "for" statement, there is no problem.问题如下,如果我注释“for”语句,没有问题。 But at the moment of using it to obtain the values of the answer and insert them to the database, this error occurs: class java.util.LinkedHashMap cannot be cast to class [name of my class].但是在使用它获取答案的值并将它们插入数据库的那一刻,出现了这个错误:class java.util.LinkedHashMap cannot be cast to ZA2F2ED4F8EBC2CBB4C21A29DC40AB61D [name class]。 And also: [my class] is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader.而且:[我的班级]在加载器 org.springframework.boot.devtools.restart.classloader.RestartClassLoader 的未命名模块中。 Can you help me to find a way to solve this problem?你能帮我找到解决这个问题的方法吗? Here is the code:这是代码:

@GetMapping("/ordenes")
    public List<WcOrden> getOrdenes() {

        List<WcOrden> ordenes = (List<WcOrden>) wc.getAll(EndpointBaseType.ORDERS.getValue(), params);
        for (WcOrden orden : ordenes) {
            log.debug("This is an order");
        }
        return ordenes;
    }

I assume wc.getAll(...) is a call to an external API.我假设wc.getAll(...)是对外部 API 的调用。

When Jackson does not have enough information about what class to deserialize to, it uses LinkedHashMap .当 Jackson 没有足够的关于要反序列化到什么 class 的信息时,它使用LinkedHashMap

In your case, it knows it is a List, but not the actual type of the elements inside the list, as explained in this article .在您的情况下,它知道它是一个列表,但不是列表中元素的实际类型,如 本文所述

So, to solve it, you can use the convertValue of the ObjectMapper:因此,要解决它,您可以使用 ObjectMapper 的 convertValue:

ObjectMapper mapper = new ObjectMapper(); // or inject it as a dependency
List<WcOrden> pojos = mapper.convertValue(wc.getAll(...), new TypeReference<List<WcOrden>>() { });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String - class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String 在使用Java 8和Java 11时,如何修复&#39;class java.util.LinkedHashMap无法强制转换为类&#39; - How to fix 'class java.util.LinkedHashMap cannot be cast to class' when using Java 8 and Java 11 如何修复“java.lang.ClassCastException:类 java.util.LinkedHashMap 无法转换为类”错误? - How to fix 'java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class' error? Java Stream:类 java.util.LinkedHashMap 不能转换为类 RealmRole - Java Stream: class java.util.LinkedHashMap cannot be cast to class RealmRole 如何修复 class java.util.LinkedHashMap 无法转换为 class Z93F725A07428BString.3321C886F46D4 错误。 - how to fix class java.util.LinkedHashMap cannot be cast to class java.lang.String error java.lang.ClassCastException:无法将java.util.LinkedHashMap转换为特定类 - java.lang.ClassCastException: Cannot cast java.util.LinkedHashMap to Specific class java.util.LinkedHashMap 不能转换为类 com.profile.model.Profile - Spring Boot - java.util.LinkedHashMap cannot be cast to class com.profile.model.Profile - Spring Boot 线程“main”java.lang.ClassCastException 中的异常:类 java.util.LinkedHashMap 无法转换为类 java.util.List - Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.util.List java.lang.ClassCastException:无法强制转换java.util.LinkedHashMap - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast ArrayList的迭代器错误:无法将java.util.LinkedHashMap强制转换为MyObject - Iterator error for ArrayList: java.util.LinkedHashMap cannot be cast to MyObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM