2
我想为数据反序列化制作一个通用方法。Java通用方法不能按预期方式工作
我的代码:
public <T> ExportedData<T> getExportData(T classType, String exportUri) {
Response response = _client.get(exportUri);
// System.out.println(response.body.toString());
ExportedData<T> exportedData = GsonSingleton.getGson().fromJson(response.body.toString(), new TypeToken<ExportedData<T>>() {
}.getType());
return exportedData;
}
的response.body
:
{"totalResults":2,"limit":50000,"offset":0,"count":2,"hasMore":false,"items":[{"DevicesIDs":"","EmailAddress":"[email protected]"},{"DevicesIDs":"","EmailAddress":"[email protected]"}]}
我称之为泛型方法的方式:
ExportedData<AccengageOutboundContact> exportedData = generalBulkHelper.getExportData(new AccengageOutboundContact(), uriLimitAndOffset);
的AccengageOutboundContact
:
public class AccengageOutboundContact {
public String EmailAddress;
public String DevicesIDs;
}
而且ExportedData
:
public class ExportedData<T> {
public int totalResults;
public int limit;
public int offset;
public int count;
public boolean hasMore;
public List<T> items;
}
我希望得到AccengageOutboundContact
对象的ArrayList。我得到的是ArrayList StringMap
。
任何想法我做错了什么?
谢谢你,会尝试,但为什么为T擦除目的? –
因为[这是Java的泛型工作原理](http://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens)。 – Kayaman
那么你是说T总是被擦除为一个对象?如果是这样,对我来说,泛型方法是失败的,我们总是可以使用对象,并将其转换为我们想要的东西,对吧? –