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

Pas*_*per 61 java jackson rest-assured

我收到以下错误:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account
Run Code Online (Sandbox Code Playgroud)

使用以下代码

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);
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ers 78

这个问题来自杰克逊.当它没有关于要反序列化的类的足够信息时,它会使用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);
Run Code Online (Sandbox Code Playgroud)


小智 27

请尝试以下方法:

POJO pojo = mapper.convertValue(singleObject, POJO.class);
Run Code Online (Sandbox Code Playgroud)

要么:

List<POJO> pojos = mapper.convertValue(
    listOfObjects,
    new TypeReference<List<POJO>>() { });
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅LinkedHashMap的转换.

  • +1 用于显示“convertValue”。我有一个案例,我需要从 json 中读取特定属性作为列表,这正是我所需要的。 (3认同)

Him*_*ant 21

我可以通过使用而不是a 来缓解JSON数组以收集LinkedHashMap对象问题的方法.这就是我的工作工作:CollectionTypeTypeReference

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;
}
Run Code Online (Sandbox Code Playgroud)

使用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;
}
Run Code Online (Sandbox Code Playgroud)


nur*_*urb 5

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

代替

List<Document> docs = obj.get("documents");
Document doc = docs.get(0)
Run Code Online (Sandbox Code Playgroud)

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

List<Document> docs = obj.get("documents");
Document doc = new Document(docs.get(0));
Run Code Online (Sandbox Code Playgroud)