获取 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);
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM