java.lang.ClassCastException:java.util.LinkedHashMap 无法转换为 com.testing.models.Account
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28821715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account
提问by Passionate Engineer
I'm getting below error:
我收到以下错误:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account
with below code
用下面的代码
final int expectedId = 1;
Test newTest = create();
int expectedResponseCode = Response.SC_OK;
ArrayList<Account> account = given().when().expect().statusCode(expectedResponseCode)
.get("accounts/" + newTest.id() + "/users")
.as(ArrayList.class);
assertThat(account.get(0).getId()).isEqualTo(expectedId);
Is there a reason why I cannot do get(0)
?
有什么我不能做的原因get(0)
吗?
采纳答案by Mark Peters
The issue's coming from Hymanson. When it doesn't have enough information on what class to deserialize to, it uses LinkedHashMap
.
问题来自Hyman逊。当它没有关于要反序列化为哪个类的足够信息时,它使用LinkedHashMap
.
Since you're not informing Hymanson of the element type of your ArrayList
, it doesn't know that you want to deserialize into an ArrayList
of Account
s. So it falls back to the default.
既然你不通知你的元素类型的Hyman逊ArrayList
,它不知道你要反序列化到ArrayList
的Account
秒。所以它回退到默认值。
Instead, you could probably use as(JsonNode.class)
, and then deal with the ObjectMapper
in a richer manner than rest-assured allows. Something like this:
相反,您可能可以使用as(JsonNode.class)
, 然后ObjectMapper
以比放心允许的更丰富的方式处理 。像这样的东西:
ObjectMapper mapper = new ObjectMapper();
JsonNode accounts = given().when().expect().statusCode(expectedResponseCode)
.get("accounts/" + newClub.getOwner().getCustId() + "/clubs")
.as(JsonNode.class);
//Hymanson's use of generics here are completely unsafe, but that's another issue
List<Account> accountList = mapper.convertValue(
accounts,
new TypeReference<List<Account>>(){}
);
assertThat(accountList.get(0).getId()).isEqualTo(expectedId);
回答by nurb
I had a similar exception (but different problem) - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.bson.Document
, and fortunately it's solved easier:
我有一个类似的异常(但不同的问题) - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.bson.Document
,幸运的是它更容易解决:
Instead of
代替
List<Document> docs = obj.get("documents");
Document doc = docs.get(0)
which gives error on second line, One can use
在第二行给出错误,可以使用
List<Document> docs = obj.get("documents");
Document doc = new Document(docs.get(0));
回答by user2578871
Try the following:
请尝试以下操作:
POJO pojo = mapper.convertValue(singleObject, POJO.class);
or:
或者:
List<POJO> pojos = mapper.convertValue(
listOfObjects,
new TypeReference<List<POJO>>() { });
See conversion of LinkedHashMapfor more information.
有关更多信息,请参阅LinkedHashMap 的转换。
回答by Himadri Pant
The way I could mitigate the JSON Array to collection of LinkedHashMap objectsproblem was by using CollectionType
rather than a TypeReference
.
This is what I did and worked:
我可以减轻JSON 数组到 LinkedHashMap 对象集合问题的方法是使用CollectionType
而不是 TypeReference
. 这就是我所做的和工作的:
public <T> List<T> jsonArrayToObjectList(String json, Class<T> tClass) throws IOException {
ObjectMapper mapper = new ObjectMapper();
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, tClass);
List<T> ts = mapper.readValue(json, listType);
LOGGER.debug("class name: {}", ts.get(0).getClass().getName());
return ts;
}
Using the TypeReference
, I was still getting an ArrayList of LinkedHashMaps, i.e. does not work:
使用TypeReference
,我仍然得到 LinkedHashMaps 的 ArrayList,即不起作用:
public <T> List<T> jsonArrayToObjectList(String json, Class<T> tClass) throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<T> ts = mapper.readValue(json, new TypeReference<List<T>>(){});
LOGGER.debug("class name: {}", ts.get(0).getClass().getName());
return ts;
}
回答by Mohamed Farghaly
I have this method for deserializing an XML and converting the type:
我有这种反序列化 XML 和转换类型的方法:
public <T> Object deserialize(String xml, Class objClass ,TypeReference<T> typeReference ) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
Object obj = xmlMapper.readValue(xml,objClass);
return xmlMapper.convertValue(obj,typeReference );
}
and this is the call:
这是电话:
List<POJO> pojos = (List<POJO>) MyUtilClass.deserialize(xml, ArrayList.class,new TypeReference< List< POJO >>(){ });