Apache 的BeanUtils 拷贝日期对象转换问题

org.apache.commons.beanutils.ConversionException: No value specified for ‘Date’

在Java对象的拷贝过程中,Apache的BeanUtils对日期的支持不是很好,但有扩展,大家可以找到它的日期转换器,注册进去就ok了。

也可以创建BeanUtils的子类,把日期转换器注册进去,以后用这个子类来copy对象:

package org.apache.ibatis.thread;

public class BeanUtilEx extends BeanUtils {

    private static Map cache = new HashMap();
    private static Log logger = LogFactory.getFactory().getInstance(BeanUtilEx.class);

    private BeanUtilEx() {
    }

    static {
        // 注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
        ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class);
        ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.util.Date.class);
        ConvertUtils.register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null),
                java.sql.Timestamp.class);
        // 注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
    }

    public static void copyProperties(Object target, Object source) throws InvocationTargetException,
            IllegalAccessException {
        // 支持对日期copy
        org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);

    }

}

测试:

public class Snippet {
    public static void main(String[] args) {
        Account account = new Account();
        account.setId(1l);
        account.setNickname("haha");
        // account.setCreateTime(new Date());
        AccountVo accountVo = new AccountVo();
        try {
            ConvertUtils.register(new org.apache.commons.beanutils.converters.DateConverter(null),
                    java.util.Date.class);

            BeanUtils.copyProperties(accountVo, account);

            System.out.println("执行");
        } catch (IllegalAccessException e) {
            System.out.println("exception");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("ex");
        }
    }
}

org.apache.commons.beanutils.converters.AbstractConverter类第132行开始

protected Object handleMissing(Class type) {

        if (useDefault || type.equals(String.class)) {
            Object value = getDefault(type);
            if (useDefault && value != null && !(type.equals(value.getClass()))) {
                try {
                    value = convertToType(type, defaultValue);
                } catch (Throwable t) {
                    log().error("    Default conversion to " + toString(type)
                            + "failed: " + t);
                }
            }
            if (log().isDebugEnabled()) {
                log().debug("    Using default "
                        + (value == null ? "" : toString(value.getClass()) + " ")
                        + "value '" + defaultValue + "'");
            }
            return value;
        }

        ConversionException cex =  new ConversionException("No value specified for '" +
                toString(type) + "'");
        if (log().isDebugEnabled()) {
            log().debug("    Throwing ConversionException: " + cex.getMessage());
            log().debug("    " + DEFAULT_CONFIG_MSG);
        }
        throw cex;

    }

参考:http://guying1028.iteye.com/blog/996869

http://guying1028.iteye.com/blog/996869

你可能感兴趣的:(java)