/** * @author xxxxxxxxxxx * @param object * 待转化类 * @param format自定义转化类型 * @return Map<String,String> 将传入的DTO转化为Map,1、Date转化为String类型的时间戳。 2、null转化为null * 3、enum通过反射使用getCode()获取code编码。4、其他类型不做转换value=toString()。5、Dto中属性名中的大写字母将被替换为 * "_小写字母"形式,为了与库中返回map一致。 */ public static Map<String, String> objectToMap(Object object, FormatField format) { if (object == null) { return null; } Map<String, String> map = new HashMap<String, String>(); Field[] declaredFields = object.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); try { String key = field.getName(); if (key.equals("serialVersionUID")) { continue; } String tmpKey=key; for(int i=0;i<key.length();i++) { if(Character.isUpperCase(key.charAt(i))) { tmpKey=tmpKey.replaceAll(""+key.charAt(i), "_"+Character.toLowerCase(key.charAt(i))); } } key=tmpKey; Object value = field.get(object); if (format != null) { value = format.format(key, field.get(object)); } if (value == null) { map.put(key, null); } else if (value instanceof Date) { map.put(key, String.valueOf(((Date) value).getTime())); } else if (value.getClass().isEnum()) { Method method = value.getClass().getMethod("getCode", null); Object val = method.invoke(value, null); map.put(key, String.valueOf(val)); } else if (value.getClass().isPrimitive()) { map.put(key, value + ""); } else { map.put(key, value.toString()); } } catch (Exception e) { e.printStackTrace(); return null; } } return map; } public static interface FormatField { public Object format(String key, Object value); }
转载:https://www.cnblogs.com/dreammyle/p/5610906.html
/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map); return obj; } public static Map<?, ?> objectToMap(Object obj) { if(obj == null) return null; return new org.apache.commons.beanutils.BeanMap(obj); } } /** * 使用Introspector进行转换 */ class B { 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; } 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进行转换 */ class C { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception { if (map == null) return null; Object obj = beanClass.newInstance(); Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } field.setAccessible(true); field.set(obj, map.get(field.getName())); } return obj; } 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; } }