BeanUtils. copyProperties Ignoring null values when copying properties

  • 2021-09-20 20:01:25
  • OfStack

BeanUtils. copyProperties ignores null values

Those who use spring are no strangers to this line of code, which is often used to copy between DTO, VO and PO.


/**
*  Full attribute copy Object 
* 
**/
BeanUtils.copyProperties(Object source, Object target)

However, this line of code will copy all attributes. Sometimes we want individual attributes not to be copied (for example, null value attributes), so we need another method:


/**
*  Ignore some attributes copy Object 
* 
**/
BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties)

The third parameter is a variable-length type, which dynamically obtains ignored attributes:


/**
 *  Gets the properties to be ignored 
 * 
 * @param source
 * @return
 */
public static String[] getNullPropertyNames (Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    PropertyDescriptor[] pds = src.getPropertyDescriptors();
 
    Set<String> emptyNames = new HashSet<>();
    for(PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        //  The judgment here can be modified according to the demand 
        if (srcValue == null) {
            emptyNames.add(pd.getName());
        }
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}

Expand 1 under:

Many times we need to convert Map to Bean in two ways:

1. Use fastjson

1. map to bean:


Map paramMap = new HashMap();
String jsonStr = JSONObject.toJSONString(paramMap);
Object infoDo = JSON.parseObject(jsonStr, Object.class);

2. bean to map:

Map < String, Object > map = JSON.parseObject(JSON.toJSONString(object),new TypeReference < Map < String,Object > > (){});

2. Use commons-beanutils

Dependent packages:


<dependency>
 <groupId>commons-beanutils</groupId>
 <artifactId>commons-beanutils</artifactId>
 <version>1.8.3</version>
</dependency>

Then call the method:


/**
 * Bean Turn map
 * 
 * @param bean
 * @return
 */
public Map describe(Object bean);
 
/**
 * map Turn bean
 * 
 * @param bean
 * @param map
 */
public void populate (Object bean, Map map);

At the same time, you can list copy objects by specified attributes:


/**
 *  Assign an object to the specified list of properties 
 *
 * @param source
 * @param target
 * @param properties
 *
 */
public static void copyWithProperties(Object source, Object target, List<String> properties) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    for (String property : properties) {
        PropertyUtils.setProperty(target, property, PropertyUtils.getProperty(source, property));
    }
}

BeanUtils When copying classes, you need to pay attention to the case where the value is null


BeanUtils.copyProperties(dest, orig);

Reference is made here to org. apache. commons. beanutils. BeanUtils;


ConvertUtils.register(new DateConverter(null), java.util.Date.class);

Add this 1 line of code to solve the problem of reporting errors when date type is null


ConvertUtils.register(new IntegerConverter(null), Integer.class);

This 1 line can solve the problem that integer value is not automatically assigned to 0 when it is empty;

In addition, double may be very special, so I didn't study it.

Note that these additional conditions should be placed before BeanUtils. copyProperties.


Related articles: