1
import com.google.gson.Gson;

public class Beanutil {
    public static void main(String args[]) {
        Map<String, Object> yourMap = new HashMap<String, Object>();
        yourMap.put("name", "Joan");
        yourMap.put("age", "309");
        Map<String, Object> secondMap = new HashMap<String, Object>();
        secondMap.put("name", "k");
        secondMap.put("age", "39");
        yourMap.put("bean",secondMap);

        YourBean bean = null;
        try {
            bean= new YourBean();
            try{
            BeanUtils.populate(bean, yourMap);
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.println(new Gson().toJson(bean));
        } catch (Throwable e) {
            e.getMessage();
        }
    }

}

bean class as follows:

public class YourBean {

    String          name    = null;
    int             age     = 0;
    public Bean2    bean;

    //getter & setters
}

public class Bean2 {

        String          name    = null;
        int             age     = 0;


        //getter and setters
    }

getting exception like

java.lang.IllegalArgumentException: Cannot invoke com.org.nlp.test.YourBean.setBean on bean class 'class com.org.nlp.test.YourBean' - argument type mismatch - had objects of type "java.util.HashMap" but expected signature "com.org.nlp.test.Bean2" at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2195) at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2108) at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1914) at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2021) at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1018) at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:823) at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:431) at com.org.nlp.test.Beanutil.main(Beanutil.java:24) Caused by: java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2127) ... 7 more {"name":"Joan","age":309}

am excepecting out like

{name=Joan, age=309, bean={name=k, age=39}}

Please help me to fix this and suggest me if their any better approach

Thanks in advance

subinksoman
  • 426
  • 4
  • 20
  • See [BeanUtils converting java.util.Map to nested bean](https://stackoverflow.com/questions/21720128/beanutils-converting-java-util-map-to-nested-bean). –  Jul 20 '17 at 08:13

2 Answers2

1

According to the error message, it seems that you need to first populate the internal Bean2:

BeanUtils.populate(bean2, secondMap);

And add the populated bean to yourMap:

yourMap.put("bean",bean2);

And then finally populate bean:

BeanUtils.populate(bean, yourMap);

This way yourMap contains the populated Bean2 member and it doesn't require to do nested populations.

Assafs
  • 3,257
  • 4
  • 26
  • 39
1

Actualy i need like this ,we can use jackson-databind

final ObjectMapper mapper = new ObjectMapper(); 

bean = mapper.convertValue(yourMap, YourBean.class);
subinksoman
  • 426
  • 4
  • 20