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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21720128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:15:59  来源:igfitidea点击:

BeanUtils converting java.util.Map to nested bean

javajavabeanspojoapache-commons-beanutils

提问by Skynet

I have a Java bean which has a field which in turn is another bean

我有一个 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}
}

I am trying to pass a map to BeanUtils to try and convert the following map into BeanOne

我正在尝试将地图传递给 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);

But it throws an error saying - Cannot invoke BeanOne.setFieldTwo on bean class 'class Bean' - argument type mismatch - had objects of type "java.util.HashMap" but expected signature "BeanTwo"

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

How can I use BeanUtils to correctly populate the inner bean ?

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

回答by Naren

Here we go you can do like this....

我们来了,你可以这样做......

BeanOne.java

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

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"));
    }

}

output will be:-

输出将是:-

fieldOneValue
fieldOne2222Value

回答by Gaetan

You should use Spring's BeanWrapper class. It supports nested properties, and optionally create inner beans for you:

您应该使用 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();

The killer feature of auto-creating inner beans is achieved thanks to wrapper.setAutoGrowNestedPaths(true). The default value is false, which means you will get a NullValueInNestedPathExceptionif an element in the property path is null.

由于wrapper.setAutoGrowNestedPaths(true). 默认值为 false,这意味着NullValueInNestedPathException如果属性路径中的元素为 null ,您将得到一个。