1. 使用org.apache.commons.beanutils转换

用到的主要jar包:commons-beanutils-1.9.3.jar。

  1. //map转java对象
  2. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass)
  3. throws Exception {
  4. if (map == null) {
  5. return null;
  6. }
  7. Object obj = beanClass.newInstance();
  8. BeanUtils.populate(obj, map);
  9. return obj;
  10. }
  11. //java对象转map
  12. public static Map<?, ?> objectToMap(Object obj) {
  13. if (obj == null)
  14. return null;
  15. return new BeanMap(obj);
  16. }

在使用该方法进行java对象转map时,可能会发生以下错误:

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer
  2. ....
  3. Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer
  4. ....

解决方法: https://blog.csdn.net/liuyunyihao/article/details/86596595

2. 使用Introspector转换

  1. //map转java对象
  2. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  3. if (map == null)
  4. return null;
  5. Object obj = beanClass.newInstance();
  6. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  7. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  8. for (PropertyDescriptor property : propertyDescriptors) {
  9. Method setter = property.getWriteMethod();
  10. if (setter != null) {
  11. setter.invoke(obj, map.get(property.getName()));
  12. }
  13. }
  14. return obj;
  15. }
  16. //java对象转map
  17. public static Map<String, Object> objectToMap(Object obj) throws Exception {
  18. if (obj == null) {
  19. return null;
  20. }
  21. Map<String, Object> map = new HashMap<String, Object>();
  22. BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
  23. PropertyDescriptor[] propertyDescriptors = beanInfo
  24. .getPropertyDescriptors();
  25. for (PropertyDescriptor property : propertyDescriptors) {
  26. String key = property.getName();
  27. if (key.compareToIgnoreCase("class") == 0) {
  28. continue;
  29. }
  30. Method getter = property.getReadMethod();
  31. Object value = getter != null ? getter.invoke(obj) : null;
  32. map.put(key, value);
  33. }
  34. return map;
  35. }

3. 使用reflect转换

  1. //map转java对象
  2. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  3. if (map == null) {
  4. return null;
  5. }
  6. Object object = beanClass.newInstance();
  7. Field[] fields = beanClass.getDeclaredFields();
  8. for (Field field : fields) {
  9. int mod = field.getModifiers();
  10. if (Modifier.isFinal(mod) || Modifier.isStatic(mod)) {
  11. continue;
  12. }
  13. field.setAccessible(true);
  14. field.set(object, map.get(field.getName()));
  15. }
  16. return object;
  17. }
  18. //java对象转map
  19. public static Map<String, Object> objectToMap(Object obj) throws Exception {
  20. if (obj == null) {
  21. return null;
  22. }
  23. Map<String, Object> map = new HashMap<String, Object>();
  24. Field[] declaredFields = obj.getClass().getDeclaredFields();
  25. for (Field field : declaredFields) {
  26. field.setAccessible(true);
  27. map.put(field.getName(), field.get(obj));
  28. }
  29. return map;
  30. }

4. 使用net.sf.cglib.beans.BeanMap转换

用到的主要jar包:cglib-2.2.2.jar。

  1. //map转java对象
  2. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  3. Object object = beanClass.newInstance();
  4. BeanMap beanMap = BeanMap.create(object);
  5. beanMap.putAll(map);
  6. return object;
  7. }
  8. //java对象转map
  9. public static Map<String, Object> objectToMap(Object obj) {
  10. Map<String, Object> map = Maps.newHashMap();
  11. if (obj != null) {
  12. BeanMap beanMap = BeanMap.create(obj);
  13. for (Object key : beanMap.keySet()) {
  14. map.put(key + "", beanMap.get(key));
  15. }
  16. }
  17. return map;
  18. }

在使用本方法进行map转java对象时,可能会发生以下错误:

  1. Exception in thread "main" java.lang.NoClassDefFoundError: org/objectweb/asm/Type
  2. ....
  3. Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type
  4. ....

解决方法:https://blog.csdn.net/liuyunyihao/article/details/86593722

5. 使用json转换

用到的主要jar包:fastjson-1.2.54.jar。

  1. //map转java对象
  2. public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
  3. String jsonStr = JSONObject.toJSONString(map);
  4. return JSONObject.parseObject(jsonStr, beanClass);
  5. }
  6. //java对象转map
  7. public static Map<String, Object> objectToMap(Object obj) {
  8. String jsonStr = JSONObject.toJSONString(obj);
  9. return JSONObject.parseObject(jsonStr);
  10. }