com.gccloud.starter.common.utils.BeanConvertUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gc-starter-common Show documentation
Show all versions of gc-starter-common Show documentation
几乎所有的模块都依赖该模块,该模块中引入了一些常用工具类、通用的bean、全局异常处理、常量定义等
package com.gccloud.starter.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils;
/**
* 类转换
*
* @author liuchengbiao
* @date 2020-07-07 10:44
*/
@Slf4j
public class BeanConvertUtils {
/**
* 类转换
*
* @param source 被转换的类
* @param targetClass 需要转为的目标类型
* @param
* @return
*/
public static T convert(Object source, Class targetClass) {
try {
if (source == null) {
return targetClass.newInstance();
}
T target = targetClass.newInstance();
BeanUtils.copyProperties(source, target);
return target;
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
}
return null;
}
/**
* 类转换
*
* @param source 被转换的类
* @param target 需要转为的目标对象,需要自己先初始化对象
*/
public static void convert(Object source, Object target) {
BeanUtils.copyProperties(source, target);
}
}