/**
* bean转换为属性map
*/
public static Map<String, Object> toPropertyMap(Object obj) {
if (obj == null) {
return null;
}
if(obj instanceof HashMap){
return (Map<String, Object>)obj;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!"class".equals(key)) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
logger.error("toPropertyMap is Error:", e);
}
return map;
}