将Map转换为Java 对象
2015-08-11 17:41
519 查看
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);
相关文章推荐
- JDK API下载
- myeclipse错误处理
- [leetcode-113]Path Sum II(java)
- 在JAVA中线程到底起到什么作用
- Java基础-ArrayList中ConcurrentModificationException错误解决
- java虚拟机原理图解1
- JAVA并发编程学习笔记之CAS操作
- Java单例设计模式
- java书籍
- 【Java代码保护技术】代码混淆
- 如何通过MyEclipse生成含有第三方包的jar包
- springMVC 【@response 返回对象自动变成json并且防止乱码】 & 【配置支持实体类中的@DateTimeFormat注解】
- jdk自带的jvm监控工具
- 字符串分割--java中String.split()用法
- 字符串排序问题
- eclipse的WEB项目无理由错误
- JAVA,Play framework 入门,打印hello word
- eclipse Android关联源码
- mybatis 与spring整合配置
- java.net.InetAddress类的使用