1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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> T copyProperties(Object source, Class<T> 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 <T> List<T> copyWithCollection(List<?> sourceList, Class<T> 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 <T> Set<T> copyWithCollection(Set<?> sourceList, Class<T> 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> T jsonStrToObject(String jsonStr, Class<T> clazz){
- return JSONObject.parseObject(jsonStr, clazz);
- }
- /**
- * JSON字符串转List
- * @param type
- * @param listString
- * @return
- */
- public static <T> List<T> listStringToOjectList(String listString,Class<T> type){
- return JSONObject.parseArray(listString, type);
- }
- }
|