1.问题
spring cloud项目开发中,使用远程调用 ,返回类型为DataResults<List>,对List进行二次封装报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Users。
2. 解决方案:
写一个通用转换类
maven依赖
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
工具类:
package com.bruce.utils;
import org.dozer.DozerBeanMapper;
import java.util.ArrayList;
import java.util.List;
public class GeneralConv {
public static <T> T conv(Object source, Class<T> destinationClass) {
if(null == source){
return null;
}
DozerBeanMapper dozerMapper = new DozerBeanMapper();
T convObj = dozerMapper.map(source, destinationClass);
return convObj;
}
public static <T> List<T> convert2List(List<?> sourceList, Class<T> destinationClass) {
List<T> destinationList =new ArrayList<>();
sourceList.forEach(source -> {
destinationList.add(GeneralConv.conv(source, destinationClass));
});
return destinationList;
}
}
3.使用
如果需要使用对象中的数据,可以自己调用工具类转换
|