最近,研究map与java对象之间的相互转换,总结了5种方法:
第一种:使用org.apache.commons.beanutils转换
用到的主要jar包:commons-beanutils-1.9.3.jar
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass)
throws Exception {
if (map == null) {
return null;
}
Object obj = beanClass.newInstance();
BeanUtils.populate(obj, map);
return obj;
}
//java对象转map
public static Map<?, ?> objectToMap(Object obj) {
if (obj == null)
return null;
return new BeanMap(obj);
}
在使用该方法进行java对象转map时,可能会发生以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at org.apache.commons.beanutils.BeanMap.createTypeTransformers(BeanMap.java:132)
at org.apache.commons.beanutils.BeanMap.<clinit>(BeanMap.java:68)
at com.example2.pojo.PojoToMap.objectToMap(PojoToMap.java:23)
at com.example2.pojo.PojoToMap.main(PojoToMap.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 16 more
解决方法见本人的另一篇文章: https://blog.csdn.net/liuyunyihao/article/details/86596595
第二种:使用Introspector转换
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null;
Object obj = beanClass.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
setter.invoke(obj, map.get(property.getName()));
}
}
return obj;
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
map.put(key, value);
}
return map;
}
第三种:使用reflect转换
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null) {
return null;
}
Object object = beanClass.newInstance();
Field[] fields = beanClass.getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if (Modifier.isFinal(mod) || Modifier.isStatic(mod)) {
continue;
}
field.setAccessible(true);
field.set(object, map.get(field.getName()));
}
return object;
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) throws Exception {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
return map;
}
第四种:使用net.sf.cglib.beans.BeanMap转换
用到的主要jar包:cglib-2.2.2.jar
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
Object object = beanClass.newInstance();
BeanMap beanMap = BeanMap.create(object);
beanMap.putAll(map);
return object;
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) {
Map<String, Object> map = Maps.newHashMap();
if (obj != null) {
BeanMap beanMap = BeanMap.create(obj);
for (Object key : beanMap.keySet()) {
map.put(key + "", beanMap.get(key));
}
}
return map;
}
在使用本方法进行map转java对象时,可能会发生以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/Type
at net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:184)
at net.sf.cglib.core.KeyFactory.<clinit>(KeyFactory.java:66)
at net.sf.cglib.beans.BeanMap$Generator.<clinit>(BeanMap.java:64)
at net.sf.cglib.beans.BeanMap.create(BeanMap.java:56)
at com.example2.pojo.MapToPojo.mapToBean(MapToPojo.java:27)
at com.example2.pojo.MapToPojo.main(MapToPojo.java:18)
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 6 more
解决方法见本人的另一篇文章:https://blog.csdn.net/liuyunyihao/article/details/86593722
第五种:使用json转换
用到的主要jar包:fastjson-1.2.54.jar
//map转java对象
public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
String jsonStr = JSONObject.toJSONString(map);
return JSONObject.parseObject(jsonStr, beanClass);
}
//java对象转map
public static Map<String, Object> objectToMap(Object obj) {
String jsonStr = JSONObject.toJSONString(obj);
return JSONObject.parseObject(jsonStr);
}
以上方法本人均实际测试通过。