map转指定类型实体类map

😂 这篇文章最后更新于1655天前,您需要注意相关的内容是否还可用。

用于转换成符合实体类属性的map

public Map<String, Object> convertMapToBeanMap(Map<String, Object> params, Class clazz) throws IntrospectionException {
    Map<String, Object> finalMap = new HashMap<>();
    BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (int i = 0; i < propertyDescriptors.length; i++) {
        PropertyDescriptor descriptor = propertyDescriptors[i];
        String propertyName = descriptor.getName();
        if (!propertyName.equals("class")) {
            if (params.containsKey(propertyName)) {
                finalMap.put(propertyName, params.get(propertyName));
            }
        }
    }
    return finalMap;
}