• 获取 bean 对象属性方法


    @Test
    public void testBeanInfo() throws IntrospectionException, InvocationTargetException, IllegalAccessException {
    BaseVo param = new BaseVo();
    param.setCode(111);
    param.setMessage("success");

    Map<String, Object> map = Maps.newConcurrentMap();

    BeanInfo beanInfo = Introspector.getBeanInfo(param.getClass());
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor property : propertyDescriptors) {
    String key = property.getName();
    // 过滤class属性
    if (!key.equals("class")) {
    // 得到property对应的getter方法
    Method getter = property.getReadMethod();
    //获取属性对应的 value
    Object value = getter.invoke(param);
    //key=bean 属性,value=bean 属性对应的 value
    map.put(key, value);
    }
    }

    System.out.println(map);
    }
  • 相关阅读:
    scp上传服务器文件
    svn一次添加所有未添加的文件
    vue 去掉#和拼接参数
    vuex状态管理
    获取页面iframe里的元素
    angular 中ng-bind-html 、$scope服务
    心态崩了
    day 8
    day 7
    day6 angularjs学习
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/12089711.html
Copyright © 2020-2023  润新知