[英]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 中,我想创建一个基于MyClass
的object
并将 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:设置如下:
.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 的原因。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.