개발 Tip
LinkedTreeMap 을 DTO로 변환 시 발생하는 문제
삐질
2019. 3. 21. 09:10
Retrofit2와 DTO로 custom클래스를 사용하는 과정중
DTO의 멤버중 , 제네릭으로 받는 멤버가있고, 그게 리스트면 ,
casting중 java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.test.test.data.entity.Puser
에러가 발생함.
해결법은,
Type type = new TypeToken<List<Puser>>() {
}.getType();
String jsonResult = gson.toJson(result.getResult().getBody());
List<Puser> pusersList = gson.fromJson(jsonResult, type);
이렇듯 TypeToken을 해당 respone DTO의 타입으로 결정하고,
gson 변환시 , 연결