更新时间:2022-11-11 13:22:36
从当前的BeanUtils v1.9.2开始,使用静态单例BeanUtils
和ConvertUtils
类时,我不认为有任何方法可以做到这一点
As of the current BeanUtils v1.9.2 I don't believe there is any way to do this when using the static singleton BeanUtils
and ConvertUtils
classes.
您可以创建BeanUtilsBean
的实例,并传递对Enum目标进行特殊处理的自定义ConvertUtilsBean
实例.
You could create an instance of BeanUtilsBean
, passing a custom ConvertUtilsBean
instance that has special handling for Enum targets.
此处显示了一个示例(不是我的示例,应归功于其作者"jeremychone"): http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/
An example is shown here (not my example, credit to its author "jeremychone"): http://www.bitsandpix.com/entry/java-beanutils-enum-support-generic-enum-converter/
Jeremy的简单实现如下:
Jeremy's simple implementation is as follows:
BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean() {
@Override
public Object convert(String value, Class clazz) {
if (clazz.isEnum()){
return Enum.valueOf(clazz, value);
}else{
return super.convert(value, clazz);
}
}
});