public class IntrospectUtil {
public static Map<String, Object> introspect(Object obj) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
BeanInfo info = Introspector.getBeanInfo(obj.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null){
Object invoke = reader.invoke(obj);
if(invoke != null){
result.put(pd.getName(), invoke);
}
}
}
return result;
}
}
这里会自动剔除值为null的
另外还有
BeanUtil.convertFieldToMap()