Springboot Map转泛型对象
import org.springframework.beans.PropertyAccessorFactory;
public abstract class AbstractGoodsProcessor<T>{
@Override
public T assembleOfBeforeCheck(Map<String, Object> map){
return this.mapToParam(map,0);
};
@Override
public abstract boolean commitOrderOfBeforeCheck(T t);
protected <Param>Param mapToParam(Map<String,Object> map, int genericIndex){
Class<Param> clazz = (Class<Param>) ((ParameterizedType) this.getClass().getGenericSuperclass())
.getActualTypeArguments()[genericIndex];
Param param = null;
try {
param = clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("创建对象失败");
}
BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(param);
beanWrapper.setPropertyValues(map,true,true);
return param;
}
}