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