BeanUtils copyProperties API to ignore null and specific propertie

让人想犯罪 __ 提交于 2019-12-21 08:16:31

问题


Spring's BeanUtils.copyProperties() provides option to ignore specific properties while copying beans:

public static void copyProperties(Object source,
                 Object target,
                 String[] ignoreProperties) throws BeansException

Does the Apache Commons BeanUtils provide a similar feature?

Also is it possible to ignore null values while using Spring's BeanUtils.copyProperties(), I see this feature with Commons BeanUtils:

Date defaultValue = null;
DateConverter converter = new DateConverter(defaultValue);
ConvertUtils.register(converter, Date.class);

Can I achieve the same with Spring's BeanUtils?


回答1:


If you want to ignore null-value you have to do it with the following line of code before copying properties:

BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0);



回答2:


In case you are using the org.springframework.beans.BeanUtils you can ignore specific properies using the method copyProperties(Object source, Object target, String... ignoreProperties). An example,

BeanUtils.copyProperties(sourceObj, targetObj, "aProperty", "another");



回答3:


This is a sample code snippet which I am using for skip the null fields while copying to target. You can add checks for specific properties using property name, value etc. I have used org.springframework.beans.BeanUtils

public static void copyNonNullProperties(Object src, Object target) {
    BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
}

public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for (PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null)
            emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}



回答4:


If you want to ignore specific properties do the following (from the BeanUtils API )

public static void copyProperties(Object source,
                                  Object target,
                                  String... ignoreProperties)
                           throws BeansException

example below : In case you want to ignore a single property named "foo" simply add as the 3rd param

BeanUtils.copyProperties(src, target,"foo");

if you want to ignore a bunch of properties, create a list and send it as the 3rd param value

final List<String> exclusionsList = new ArrayList<>();
        exclusionsList.add("foo");
        exclusionsList.add("bar");
BeanUtils.copyProperties(src, target,exclusionsList);   


来源:https://stackoverflow.com/questions/17417345/beanutils-copyproperties-api-to-ignore-null-and-specific-propertie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!