PropertyUtilsBean 将bean转成map

public static Map<String,String> beanToMap(Object bean) {
Map<String,String> params =Maps.newHashMap();
try {
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(bean);
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
if (!"class".equals(name)) {
Object value = propertyUtilsBean.getNestedProperty(bean, name);

if(value!=null && value.toString()!=null){
if(isJavaClass(value.getClass()) && !(value instanceof Collection<?>) ){
params.put(name,value.toString());
}else{

if(value==null||name.startsWith("global")){
continue;
}
String jsonObject=JsonMapper.getInstance().toJsonString(value);
if(null==jsonObject){
params.put(name,value.toString());
}else{
params.put(name,jsonObject.toString());
}
}
}

}
}
} catch (Exception e) {
e.printStackTrace();
}
return params;
}

原文地址:https://www.cnblogs.com/ChenD/p/6700018.html