博客详情

java中将bean转为map (原创)

作者: 朝如青丝暮成雪
发布时间:2019-10-18 11:39:51  文章分类:java编程   阅读(1219)  评论(0)

java中将bean转为map



import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class MyBeanUtil {
	
	/**
     *  将bean转换成map
     * @Description:   
     */
    public static Map<String, Object> beanToMap(Object o) {
        if (o == null) {
            return null;
        }
       
        Map<String, Object> map = new HashMap<String, Object>();
        BeanInfo info = null;
        try {
            info = Introspector.getBeanInfo(o.getClass());
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
            Method reader = pd.getReadMethod();
            if (reader != null && !"class".equals(pd.getName())) {
                try {
                    map.put(pd.getName(), reader.invoke(o));
                } catch (Exception e) {
                   e.printStackTrace();
                }
            }
        }
        return map;

    }

}


关键字:  java  bean  map

上一篇:lombok插件的使用

下一篇:HttpClientUtil

评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号