简体   繁体   English

java.lang.ClassCastException: myClass 不能转换为 class java.util.List

[英]java.lang.ClassCastException: myClass cannot be cast to class java.util.List

I have a class setup called MyClass that holds two lists:我有一个名为MyClass的 class 设置,其中包含两个列表:

public class MyClass {
    List<String> list1;
    List<Descriptor> list2;

    public List<String> getList1() { return list1; }

    public List<Descriptor> getList2() { return list2; }

    public void setList1(List<String> list1) { this.list1 = list1; }

    public void setList2(List<Descriptor> list2) { this.list2 = list2; }
}

In a different class, I want to create an object based off of MyClass and supply that object to a method在不同的 class 中,我想创建一个基于MyClassobject并将 object 提供给方法

MyClass class = new MyClass();
class.setList1(someList);
class.setList2(otherList);
return Response.status(HttpStatus.OK_200).entity(class).build();

the method .entity takes an object but I get thrown the java.lang.ClassCastException error and can't figure out why.方法.entity采用 object 但我得到了java.lang.ClassCastException错误并且无法弄清楚原因。

Everything supplied is a List so I don't understand why this error is getting thrown提供的所有内容都是一个列表,所以我不明白为什么会抛出这个错误

You're saying: MyClass (the entity) consists of lists, therefore it is a list.您是说: MyClass (实体)由列表组成,因此它是一个列表。

That's as crazy as saying: My house is made of bricks, therefore my house is a brick.就像那句话一样疯狂:我的房子是砖砌的,所以我的房子是砖。

The central job here is taking the thing you pass to the entity() method and turning that into a sack of bytes, because the HTTP protocol sends bytes.这里的核心工作是将传递给entity()方法的内容转换为一袋字节,因为 HTTP 协议发送字节。 How does that work?这是如何运作的? That's where you need to learn how your tools and libraries work: I'm assuming that you're using Jersey/JAX-RS here.这就是您需要了解您的工具和库如何工作的地方:我假设您在这里使用的是 Jersey/JAX-RS。 That is set up as follows:设置如下:

  • You can send any object at all back.您可以将任何 object 全部退回。
  • However, that object is spooled through a bunch of registered 'translators' that each have the ability to translate a subset of object types into a stream of bytes.然而,object 是通过一堆注册的“翻译器”进行假脱机的,每个翻译器都能够将 object 类型的子集翻译成 stream 字节。 Out of the box, the system knows how to 'translate' Strings (that's trivial - convert the strings to bytes using .getBytes(someCharsetEncoding) , voila - bytes) and a few other things.开箱即用,系统知道如何“翻译”字符串(这很简单——使用.getBytes(someCharsetEncoding)将字符串转换为字节,瞧——字节)和其他一些东西。 But you can add on more translators if you want.但是如果你愿意,你可以添加更多的翻译。 That is why the entity() method takes any Object.这就是entity()方法采用任何 Object 的原因。
  • Some registered translator assumes they are lists when they are not, causing your error.一些注册的翻译人员认为它们不是列表,从而导致您的错误。 (Because you have an instance of MyClass , which isn't a list. Sure, all its fields are lists, but, a house is not a brick). (因为你有一个MyClass的实例,它不是一个列表。当然,它的所有字段都是列表,但是,房子不是砖头)。

But the solution (probably) isn't in fixing that 'translator'.但解决方案(可能)不在于修复那个“翻译器”。 First think it through: What are you actually trying to do here?首先想一想:你到底想在这里什么? HTTP just sends bytes, so what bytes do you want to send? HTTP 只是发送字节,那么你要发送什么字节呢? That MyClass object needs to be turned into bytes one way or another. MyClass object 需要以一种或另一种方式转换为字节。 How should that go?那go应该怎么做? What service is calling this?什么服务调用这个? What does it do?它有什么作用?

Here's one example answer:这是一个示例答案:

Some website has javascript that makes an AJAX call to this endpoint and turns whatever it receives into a javascript object by parsing it with a JSON library, or just eval() ing it. Some website has javascript that makes an AJAX call to this endpoint and turns whatever it receives into a javascript object by parsing it with a JSON library, or just eval() ing it.

If that is your answer, that you want your MyClass entity to be turned into JSON.如果这是您的答案,您希望将MyClass实体变成 JSON。 Jackson can do that for you. Jackson可以为您做到这一点。

暂无
暂无

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

相关问题 线程“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.lang.String[] 不能转换为 java.util.List - java.lang.ClassCastException: java.lang.String[] cannot be cast to java.util.List 线程“main”中的异常 java.lang.ClassCastException:class org.json.simple.JSONObject 无法转换为 class java.util.List - Exception in thread "main" java.lang.ClassCastException: class org.json.simple.JSONObject cannot be cast to class java.util.List 如何解决这个java.lang.ClassCastException:[B不能强制转换为java.util.List - How to fix this java.lang.ClassCastException: [B cannot be cast to java.util.List java.lang.ClassCastException:[使用 Redis 时无法将 B 转换为 java.util.List - java.lang.ClassCastException: [B cannot be cast to java.util.List when using Redis java.lang.ClassCastException: Model.Film 不能转换为 java.util.List - java.lang.ClassCastException: Model.Film cannot be cast to java.util.List 深入JUnit参数化测试运行器:java.lang.ClassCastException:[Ljava.lang.Object; 无法转换为java.util.List - deep within JUnit Parameterized test runner: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List java.lang.ClassCastException:org.hibernate.internal.SQLQueryImpl无法转换为java.util.List如何修复 - java.lang.ClassCastException: org.hibernate.internal.SQLQueryImpl cannot be cast to java.util.List how to fix 线程“主”java.lang.ClassCastException 中的异常:www.logisense.com.pogos.wizardNewProduct.ProductWorkingDates 不能转换为 Z93F725A07423FEList21C846D48。 - Exception in thread "main" java.lang.ClassCastException: www.logisense.com.pogos.wizardNewProduct.ProductWorkingDates cannot be cast to java.util.List 请求处理失败; 嵌套异常是 java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MyClass - Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to MyClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM