利用beanutils map 转换为bean, 奇怪的是转换结果属性结果为空
工具类:
public static final <T> List<T> mapsToObjects(List<Map<String,Object>> from, Class<T> clazz){
if (null == from) {
return null;
}
List<T> collect = (List<T>) from
.stream()
.map(x -> {
T t=null;
try {
t= clazz.newInstance();
org.apache.commons.beanutils.BeanUtils.populate(t, x);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}).collect(toList());
return collect;
}
后来排查,是javabean有问题,正常的javabean是getter setter方法,测试的时候传入了一个错误的bean
因为这个bean是mybatis 插件生成的,不是标准的bean,
@TableName("fb_user_agent")
public class MyClass2 implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID
*/
private Long id;
/**
* 备注说明
*/
private String remark;
public Long getId() {
return id;
}
public MyClass2 setId(Long id) {
this.id = id;
return this;
}
public String getRemark() {
return remark;
}
public MyClass2 setRemark(String remark) {
this.remark = remark;
return this;
}
}