I have tried BeanUtils as well as PropertyDescriptors because I did not have the information on the class that was passed to my method. I did not even know the data types of the properties passed and so setting values became difficult. I know that BeanUtils should do the conversion automatically for the property and set it but it was not saving the data for me. So finally, I had to rely on getting the fields. Here is what I did:
Field[] fields = className.getDeclaredFields();
for (int i=0; i<fields.length ;i++)
{
String element = fields[i].getName();
String propertyType = fields[i].getType().getName();
fields[i].setAccessible(true);
if(propertyType.equalsIgnoreCase("java.lang.Integer"))
{
fields[i].set(mypojoObj, Integer.parseInt(parameterValue));
}
else
{
fields[i].set(mypojoObj, parameterValue);
}
}
I did a similar switch-case to convert all property types to correct types. When fetching from the page, request.getParameter(paramname) was always returning String so this conversion worked for me. Any better options for direct data conversion will be really helpful.