将Map转换为Java 对象
public class MapUtil { public static Object convert2Object(Class clazz,Map<String,Object[]> map) throws IntrospectionException, InstantiationException, IllegalAccessException{ BeanInfo bi = Introspector.getBeanInfo(clazz); Object obj = clazz.newInstance(); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); String pName; for(PropertyDescriptor pd:pds){ pName = pd.getName(); if(map.containsKey(pName)){ try { pd.getWriteMethod().invoke(obj, map.get(pName)[0]); } catch (Exception ex) { Logger.getLogger(MapUtil.class.getName()).log(Level.SEVERE, null, ex); } } } return obj; } }
使用方法:
Map<String,Object[]> map = request.getParameterMap(); Request req = (Request)MapUtil.convert2Object(Request.class, map);