Generics 泛型JSON反序列化

Generics 泛型JSON反序列化,generics,jackson,Generics,Jackson,我有这门课: public static class Version { @Getter private float version; @Getter private String url; @Getter private String label; } 我正试图编写一个通用方法,用于从JSON反序列化这个类和其他类的实例。 到目前为止,我已经: private <T> List<T> deserializeJSONList(String jso

我有这门课:

public static class Version {
    @Getter private float version;
    @Getter private String url;
    @Getter private String label;
}
我正试图编写一个通用方法,用于从JSON反序列化这个类和其他类的实例。 到目前为止,我已经:

private <T> List<T> deserializeJSONList(String json) {
    try {
        TypeReference reference = new TypeReference<List<T>>() {};
        return (List<T>) objectMapper.readValue(json, reference);
    } catch (JsonProcessingException e) {
        throw new RequestException(e);
    }
}
私有列表反序列化JSONList(字符串json){
试一试{
TypeReference=新的TypeReference(){};
返回(列表)objectMapper.readValue(json,引用);
}捕获(JsonProcessingException e){
抛出新的RequestException(e);
}
}
我称之为:

List<Version> versions = deserializeJSONList(response.body());
List versions=deserializeJSONList(response.body());
问题是返回的列表根本不包含版本实例,而是包含LinkedHashMap的实例,其中包含表示字段的键/值对

如何使用泛型方法返回包含正确类型对象的列表?

使用此方法

public static <T> List<T> fromJsonToList(String json, Class<T> clazz) throws IOException {
        CollectionType customClassCollection = objectMapper.getTypeFactory().constructCollectionType(List.class, clazz);
        return objectMapper.readValue(json, customClassCollection);
    }
来自JSONTOLIST的公共静态列表(字符串json,类clazz)抛出IOException{
CollectionType customClassCollection=objectMapper.getTypeFactory().ConstructionCollectionType(List.class,clazz);
返回objectMapper.readValue(json,customClassCollection);
}

查看相关问题:,