java对象转map_java中实现map与对象相互转换的几种实现

2022-08-18 20:25:33 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

最近,研究map与java对象之间的相互转换,总结了5种方法:

第一种:使用org.apache.commons.beanutils转换

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

//map转java对象

public static Object mapToObject(Map 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.(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

第二种:使用Introspector转换

//map转java对象

public static Object mapToObject(Map 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 objectToMap(Object obj) throws Exception {

if (obj == null) {

return null;

}

Map map = new HashMap();

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 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 objectToMap(Object obj) throws Exception {

if (obj == null) {

return null;

}

Map map = new HashMap();

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 map, Class> beanClass) throws Exception {

Object object = beanClass.newInstance();

BeanMap beanMap = BeanMap.create(object);

beanMap.putAll(map);

return object;

}

//java对象转map

public static Map objectToMap(Object obj) {

Map 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.(KeyFactory.java:66)

at net.sf.cglib.beans.BeanMap$Generator.(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

第五种:使用json转换

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

//map转java对象

public static Object mapToObject(Map map, Class> beanClass) throws Exception {

String jsonStr = JSONObject.toJSONString(map);

return JSONObject.parseObject(jsonStr, beanClass);

}

//java对象转map

public static Map objectToMap(Object obj) {

String jsonStr = JSONObject.toJSONString(obj);

return JSONObject.parseObject(jsonStr);

}

以上方法本人均实际测试通过。到此这篇关于java中实现map与对象相互转换的几种实现的文章就介绍到这了,更多相关java map与对象相互转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135572.html原文链接:https://javaforall.cn

0 人点赞