package com.usky.utils; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import java.util.List; import java.util.Set; import java.util.stream.Collectors; /** * @author laowo * @version v1.0 * @date 2020/3/9 12:49 * @description TODO **/ @Slf4j public class BeanHelp { public static T copyProperties(Object source, Class target){ try { T t = target.newInstance(); BeanUtils.copyProperties(source, t); return t; } catch (Exception e) { log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e); //logger.error(); // throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR); } return null; } public static List copyWithCollection(List sourceList, Class target){ try { return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toList()); } catch (Exception e) { log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e); // throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR); } return null; } public static Set copyWithCollection(Set sourceList, Class target){ try { return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toSet()); } catch (Exception e) { } return null; } /** * JSON字符串转对象 * @param jsonStr * @param clazz * @return */ public static T jsonStrToObject(String jsonStr, Class clazz){ return JSONObject.parseObject(jsonStr, clazz); } /** * JSON字符串转List * @param type * @param listString * @return */ public static List listStringToOjectList(String listString,Class type){ return JSONObject.parseArray(listString, type); } }