java.lang.ClassCastException:java.util.LinkedHashMap无法强制转换为com.testing.models.Account

问题描述 投票:51回答:5

我收到以下错误:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account

使用以下代码

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);

有什么理由不能做get(0)吗?

java jackson rest-assured
5个回答
67
投票

这个问题来自杰克逊。如果没有关于要反序列化的类的足够信息,则使用LinkedHashMap

既然你没有通知杰克逊你的ArrayList的元素类型,它不知道你想要反序列化为ArrayListAccount。所以它回退到默认值。

相反,你可以使用as(JsonNode.class),然后以比放心的允许更丰富的方式处理ObjectMapper。像这样的东西:

ObjectMapper mapper = new ObjectMapper();

JsonNode accounts = given().when().expect().statusCode(expectedResponseCode)
    .get("accounts/" + newClub.getOwner().getCustId() + "/clubs")
    .as(JsonNode.class);


//Jackson'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);

25
投票

请尝试以下方法:

POJO pojo = mapper.convertValue(singleObject, POJO.class);

要么:

List<POJO> pojos = mapper.convertValue(
    listOfObjects,
    new TypeReference<List<POJO>>() { });

有关更多信息,请参阅conversion of LinkedHashMap


18
投票

我可以通过使用CollectionType而不是TypeReference来缓解JSON数组到LinkedHashMap对象集合的方式。这就是我的工作和工作:

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;
}

使用TypeReference,我仍然得到LinkedHashMap的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;
}

4
投票

我有一个类似的例外(但不同的问题) - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.bson.Document,幸运的是它更容易解决:

代替

List<Document> docs = obj.get("documents");
Document doc = docs.get(0)

在第二行给出错误,可以使用

List<Document> docs = obj.get("documents");
Document doc = new Document(docs.get(0));

0
投票

我有这种方法反序列化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 );   
}

这就是电话:

List<POJO> pojos = (List<POJO>) MyUtilClass.deserialize(xml, ArrayList.class,new TypeReference< List< POJO >>(){ });
© www.soinside.com 2019 - 2024. All rights reserved.