为避免返回给前端的字段信息太多,在缓解前、后端通信的带宽压力的前提下,对不必要的字段的信息进行不返回时,entity层对象需要向vo层对象进行转换,同事尽量减少geetter与setter方法的编码。
1. ConvertUtils工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
public class ConvertUtils {
private static Logger LOGGER = LoggerFactory.getLogger(ConvertUtils.class);
// 对象转换
public static <T> T sourceToTarget(Object source, Class<T> target){
if(source == null){
return null;
}
T targetObject = null;
try {
targetObject = target.newInstance();
BeanUtils.copyProperties(source, targetObject);
} catch (Exception e) {
LOGGER.error("convert error ", e);
}
return targetObject;
}
public static <T> List<T> sourceToTarget(Collection<?> sourceLis