1. 使用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
....
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer
....
解决方法: https://blog.csdn.net/liuyunyihao/article/details/86596595
2. 使用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;
}
3. 使用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;
}
4. 使用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
....
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type
....
解决方法:https://blog.csdn.net/liuyunyihao/article/details/86593722
5. 使用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);
}