Java 通常会在编译时擦除Generics
数据,但有可能获得该信息(JacksonObjectMapper
做得很好)。
我的问题:我有一个具有 List 属性的类:
public class User {
public List<Long> listProp;//it is public only to keep the example simple
}
如何获得正确的TypeReference
(或JavaType
?),以便我可以以编程方式将 JSON 字符串映射到正确的列表类型,具有Class
类的实例 (User.class) 和属性名称 (listProp)?我的意思是:
TypeReference typeReference = ...;//how to get the typeReference?
List<Long> correctList = om.readValue(jsonEntry.getValue(), typeReference);//this should return a List<Long> and not eg. a List<Integer>