java中将bean转为map
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class MyBeanUtil {
/**
* 将bean转换成map
* @Description:
*/
public static Map<String, Object> beanToMap(Object o) {
if (o == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
BeanInfo info = null;
try {
info = Introspector.getBeanInfo(o.getClass());
} catch (IntrospectionException e) {
e.printStackTrace();
}
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method reader = pd.getReadMethod();
if (reader != null && !"class".equals(pd.getName())) {
try {
map.put(pd.getName(), reader.invoke(o));
} catch (Exception e) {
e.printStackTrace();
}
}
}
return map;
}
}
上一篇:lombok插件的使用
下一篇:HttpClientUtil


阅读排行


Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1