spring cloude fegin远程调用接口返回linkedHashMap问题

spring cloud项目开发中,使用fegin进行远程调用 ,返回类型为PageJsonResult>,对List进行二次封装(for (BusinessLogVO resultInfo : resultInfos){})报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX。

spring cloude fegin远程调用接口返回linkedHashMap问题_第1张图片

解决方案:

写一个通用转换类

maven依赖


    net.sf.dozer
    dozer
    5.5.1
public class GeneralConv {
    /**
     * 对象通用转换
     *
     * @param source           源对象
     * @param destinationClass 目标类
     * @param 
     * @return 返回得到destinationClass
     */
    public static  T conv(Object source, Class destinationClass) {
        if(null == source){
            return null;
        }
        DozerBeanMapper dozerMapper = new DozerBeanMapper();
        T convObj = dozerMapper.map(source, destinationClass);
        return convObj;
    }

    /**
     * 集合转换
     *
     * @param sourceList       源集合
     * @param destinationClass 目标类
     * @param 
     * @return 返回得到destinationClass的集合结果
     */
    public static  List convert2List(List sourceList, Class destinationClass) {
        List destinationList = Lists.newArrayList();
        sourceList.forEach(source -> {
            destinationList.add(GeneralConv.conv(source, destinationClass));
        });
        return destinationList;
    }
}

转换用法

List resultInfos = GeneralConv.convert2List((List)result.getInfo(),BusinessLogVO.class);

你可能感兴趣的:(springcloud)