我發現了很多類似的問題,但沒有一個幫助過。通用反序列化Gson類型令牌問題
這工作:
public class Assets<T> {
public T getAndDeserializeAsset(String endpoint, String assetId, Client client){
Response response = client.get(endpoint+assetId);
Gson gson = new Gson();
T asset = gson.fromJson(response.body, new TypeToken<Email>(){}.getType());
return asset;
}
}
這不:
public class Assets<T> {
public T getAndDeserializeAsset(String endpoint, String assetId, Client client){
Response response = client.get(endpoint+assetId);
Gson gson = new Gson();
T asset = gson.fromJson(response.body, new TypeToken<T>(){}.getType());
return asset;
}
}
我已經根據官方文檔做了,所以我不知道爲什麼它不起作用。
錯誤:
Exception in thread "main" java.lang.ClassCastException: com.rest.api.Response cannot be cast to model.Email
at main.Main.main(Main.java:34)