话不多说,直接上代码:
/**
* 将对应实体类参数转为map类型同时去除至为null的字段
*
* @param o
* @return
*/
public static Map<String, String> objectToMap(Object o) {
Map<String, String> map = new HashMap<>();
if (!ObjectUtils.isEmpty(o)) {
Class c = o.getClass();
Field[] fields = c.getDeclaredFields();
if (fields != null) {
for (Field f : fields) {
f.setAccessible(true);//设置可访问权限 可获取到私有属性的value
String name = f.getName();
//获取属性的值
Object value = null;
try {
value = f.get(o);
} catch (IllegalAccessException e) {
logger.error("----> IllegalAccessException_" + e.getMessage());
}
if (!ObjectUtils.isEmpty(value)) {
map.put(name, value.toString());
}
}
}
}
return map;
}
如有不妥之处,望各位大佬及时纠正