java实体类转成Map

时间:2025-02-16 10:29:39


将实体类转成Map


	private static Logger LOGGER = ();

	// Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
	public static Map<String, Object> transBean2Map(Object obj) {
		if (obj == null) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			BeanInfo beanInfo = (());
			PropertyDescriptor[] propertyDescriptors = ();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = ();
				// 过滤class属性
				if (!("class")) {
					// 得到property对应的getter方法
					Method getter = ();
					Object value = (obj);

					(key, value);
				}

			}
		} catch (Exception e) {
			("transBean2Map Error {}" ,e);
		}
		return map;

	}