Apache 的BeanUtils copyProperties(limitVo, limit)拷贝日期对象转换问题

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

Apache的BeanUtils对日期的支持不是很好,但可以扩展,可以找到它的日期转换器,注册进去就ok了。

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

第一种方式:

package com.qb.fit.fortune.util;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 扩展Apache BeanUtils
 * 解决:org.apache.commons.beanutils.ConversionException: No value specified for ‘Date’
 * 在Java对象的拷贝过程中,Apache的BeanUtils对日期的支持不是很好
 */
public class BeanUtilEx extends BeanUtils {

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

    private BeanUtilEx() {
    }

    static {       

        // 注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
        ConvertUtils.register(new DateConverter(null), Date.class);
    }

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

第二中方式

package com.qb.fit.fortune.util;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 扩展Apache BeanUtils
 * 解决:org.apache.commons.beanutils.ConversionException: No value specified for ‘Date’
 * 在Java对象的拷贝过程中,Apache的BeanUtils对日期的支持不是很好
 */
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 SqlDateConverter(null), java.sql.Date.class);
        ConvertUtils.register(new SqlDateConverter(null), java.util.Date.class);
        ConvertUtils.register(new 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);
    }
}

 

你可能感兴趣的:(util,java)