public static <T> Map<String, Object> bean2Map(Object object) {
Map<String,Object> maps = new HashMap<>();
try{
Field[] fields = object.getClass().getDeclaredFields();
if (fields!=null && fields.length>0){
for (Field field:fields){
int a = field.getModifiers();
log.info("field name {} modifier is {}",field.getName(),a);
//当属性的修饰符为private,需要setAccessible(true);
if (!field.isAccessible()){
field.setAccessible(true);
}
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
if (null == annotation){
Object value = field.get(object);
maps.put(field.getName(),value);
}else {
log.info("annotation json property {}",annotation.value());
maps.put(annotation.value(),field.get(object));
}
}
}
}catch (Exception ex){
ex.printStackTrace();
}
return maps;
}
JAVA Bean转换为MAP,兼容@JsonProperty
最新推荐文章于 2023-12-05 18:09:28 发布