I am getting class cast exception at line 7 in the code below. I have written line 1-3 to provide data that comes from a rest service call and these line can’t be changed from my side. I must cast the response to List
JavaScript
x
List<Integer> lstInt = new ArrayList<Integer>();
lstInt.add(1);
lstInt.add(2);
GenericEntity<List<Integer>> gEntity = new GenericEntity<List<Integer>>(lstInt) {};
System.out.println(gEntity.getClass()); //prints com.org.dept.proj.MyClass$12
List<Integer> output = (List<Integer>) gEntity; // class cast exception
System.out.println(output);
Advertisement
Answer
Thank you @Michael. I had to run a getEntity
on gEntity
and then cast it as per the documentation
JavaScript
List<Integer> lstInt = new ArrayList<Integer>();
lstInt.add(1);
lstInt.add(2);
GenericEntity<List<Integer>> gEntity = new GenericEntity<List<Integer>>(lstInt) {};
System.out.println(gEntity.getEntity());
List<Integer> output = (List<Integer>) gEntity.getEntity();
System.out.println(output);