BeanUtils将java.util.Map转换为嵌套bean

时间:2014-02-12 06:16:55

标签: java javabeans pojo apache-commons-beanutils

我有一个Java bean,它有一个字段,而这个字段又是另一个bean

public class BeanOne {
   private String fieldOne;
   private BeanTwo fieldTwo;

   public String getFieldOne() {return this.fieldOne;}  
   public void setFieldOne(String fieldOne){this.fieldOne = fieldOne}

   public BeanTwo getFieldTwo() {return this.fieldTwo;}  
   public void setFieldTwo(BeanTwo fieldTwo){this.fieldTwo = fieldTwo}
}

public class BeanTwo {
   private String fieldOne;

   public String getFieldOne() {return this.fieldOne;}  
   public void setFieldOne(String fieldOne){this.fieldOne = fieldOne}
}

我正在尝试将地图传递给BeanUtils以尝试将以下地图转换为BeanOne

Map beanOneMap = new HashMap<String, Object>();
beanOneMap.put("fieldOne", "fieldOneValue");
Map beanTwoMap = new HashMap<String, Object>();
beanTwoMap.put("fieldOne", "fieldOneValue");
beanOneMap.put("fieldTwo", beanTwoMap);

BeanOne beanOne = new BeanOne();
BeanUtils.populate(beanOne, beanOneMap);

但它引发了一个错误 - Cannot invoke BeanOne.setFieldTwo on bean class 'class Bean' - argument type mismatch - had objects of type "java.util.HashMap" but expected signature "BeanTwo"

如何使用BeanUtils正确填充内部bean?

2 个答案:

答案 0 :(得分:9)

你应该使用Spring的BeanWrapper类。它支持嵌套属性,并可选择为您创建内部bean:

BeanOne one = new BeanOne();
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(one);
wrapper.setAutoGrowNestedPaths(true);

Map<String, Object> map = new HashMap<>();
map.put("fieldOne", "fieldOneValue");
map.put("fieldTwo.fieldOne", "fieldOneValue");

wrapper.setPropertyValues(map);

assertEquals("fieldOneValue", one.getFieldOne());
BeanTwo two = one.getFieldTwo();
assertNotNull(two);
assertEquals("fieldOneValue", two.getFieldOne();

通过wrapper.setAutoGrowNestedPaths(true)实现了自动创建内部bean的杀手级功能。默认值为false,这意味着如果属性路径中的元素为null,您将获得NullValueInNestedPathException

答案 1 :(得分:6)

我们在这里你可以这样做....

BeanOne.java

import java.util.Map;

public class BeanOne {
    private String fieldOne;
    private Map<String,BeanTwo> fieldTwo;
    public Map<String, BeanTwo> getFieldTwo() {
        return fieldTwo;
    }

    public void setFieldTwo(Map<String, BeanTwo> fieldTwo) {
        this.fieldTwo = fieldTwo;
    }

    public String getFieldOne() {
        return this.fieldOne;
    }

    public void setFieldOne(String fieldOne) {
        this.fieldOne = fieldOne;
    }
}

BeanTwo.java

public class BeanTwo {
    private String fieldOne;

    public String getFieldOne() {
        return this.fieldOne;
    }

    public void setFieldOne(String fieldOne) {
        this.fieldOne = fieldOne;
    }
}

Tester.java

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

import org.apache.commons.beanutils.BeanUtils;

public class Tester {
    public static void main(String[] args) throws IllegalAccessException,
            InvocationTargetException {
        Map beanTwoMap = new HashMap();
        beanTwoMap.put("fieldOne", "fieldOne2222Value");
        Map beanOneMap = new HashMap();
        beanOneMap.put("fieldOne", "fieldOneValue");
        beanOneMap.put("fieldTwo", beanTwoMap);
        BeanOne beanOne = new BeanOne();
        BeanUtils.populate(beanOne, beanOneMap);
        System.out.println(beanOne.getFieldOne());
        System.out.println(beanOne.getFieldTwo().get("fieldOne"));
    }

}

输出将是: -

fieldOneValue
fieldOne2222Value