/**
* JavaBean对象转化成Map对象
*
* @param javaBean
* @return
* @author
*/
public static Map<String, Object> bean2Map(Object javaBean) {
  Map<String, Object> map = new HashMap<String, Object>();

  try {
    // 获取javaBean属性
    BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());

    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    if (propertyDescriptors != null && propertyDescriptors.length > 0) {
      String propertyName = null; // javaBean属性名
      Object propertyValue = null; // javaBean属性值
      for (PropertyDescriptor pd : propertyDescriptors) {
        propertyName = pd.getName();
        if (!propertyName.equals("class")) {
          Method readMethod = pd.getReadMethod();
          propertyValue = readMethod.invoke(javaBean, new Object[0]);

          map.put(propertyName, propertyValue);
        }
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

  return map;
}

相关文章:

  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2021-05-23
  • 2021-11-15
猜你喜欢
  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2021-12-09
  • 2022-03-05
相关资源
相似解决方案