The Introspector.getBeanInfo() method is used

public static Map convertBean(Object bean) throws IntrospectionException,
  IllegalAccessException, InvocationTargetException {


  Map returnMap = new HashMap();

 

  Class type = bean.getClass(); // get the name of the object

 

  BeanInfo beanInfo = Introspector.getBeanInfo(type);
  // Introspect on the Java Bean for all its properties, exposed methods and events

 

 

  PropertyDescriptor[] propertyDescriptors = beanInfo .getPropertyDescriptors();
  // 获得 beans PropertyDescriptor

 

  for (int i = 0; i < propertyDescriptors.length; i++) {
   PropertyDescriptor descriptor = propertyDescriptors[i];

 

   String propertyName = descriptor.getName();
   // get all object properties worth name

 

   if (!propertyName.equals("class")) {
    Method readMethod = descriptor.getReadMethod();
    Object result = readMethod.invoke(bean, new Object[0]);
    if (result != null) {
     returnMap.put(propertyName, result);
    } else {
     returnMap.put(propertyName, "");
    }
   }
  }
  return returnMap;
 }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326391228&siteId=291194637