How to create a custom converter for copy properties
Now we would to introduce a short tutorial on how to create a custom converter for type String for the Common Beanutils Framework version 1.8.
If we would to copy the value of the propertry of an object, but for the Strings, we want to initialize to empty String the property when the original bean have the property setted to null.
In the first step we create a custom String converter class:
public class OurStringConverter implements Converter {
public Object convert(Class type, Object value) {
if (value == null) {
return (String)"";
} else {
return (value.toString());
}
}
}
Now we must register this converter:
ConvertUtilsBean convertUtils = new ConvertUtilsBean();
convertUtils.register(new OurStringConverter(), java.lang.String.class);
In the last step, we can use our ConvertUtilsBean with the new String converter registered.
BeanUtilsBean beanUtils = BeanUtilsBean(convertUtils, new PropertyUtilsBean());
beanUtils.copyProperties(dest, source);
Note that you can register all the custom converters you wants…

Handy little tip.
Just got me out of a little bit of bother.
Thanks.