我正在使用HeaderColumnNameMappingStrategy将带有标头的csv文件映射到JavaBean.字符串值解析正常,但csv中的任何"true"或"false"值都不映射到JavaBean,我从PropertyDescriptor获得以下异常:
java.lang.IllegalArgumentException: argument type mismatch
Run Code Online (Sandbox Code Playgroud)
它出现的代码在CsvToBean第64行:
protected T processLine(MappingStrategy<T> mapper, String[] line) throws
IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
T bean = mapper.createBean();
for(int col = 0; col < line.length; col++) {
String value = line[col];
PropertyDescriptor prop = mapper.findDescriptor(col);
if (null != prop) {
Object obj = convertValue(value, prop);
// this is where exception is thrown for a "true" value in csv
prop.getWriteMethod().invoke(bean, new Object[] {obj});
}
}
return bean;
}
protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws
InstantiationException, IllegalAccessException {
Class<?> cls = desc.getPropertyEditorClass();
if (null != cls) return (PropertyEditor) cls.newInstance();
return getPropertyEditorValue(desc.getPropertyType());
}
Run Code Online (Sandbox Code Playgroud)
我可以确认(通过调试器)此时正确检索到setter方法id.
问题出现在desc.getPropertyEditorClass()中,因为它返回null.我假设原始类型和它的包装器都受支持.他们不是吗?
我遇到了同样的问题.最干净的方法可能是像上面的pritam一样覆盖getPropertyEditor,并为您的特定对象返回一个自定义PropertyEditor.快速而肮脏的方法是以匿名类形式覆盖convertValue,如下所示:
CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){
@Override
protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {
if (prop.getName().equals("myWhatever")) {
// return an custom object based on the incoming value
return new MyWhatever((String)value);
}
return super.convertValue(value, prop);
}
};
Run Code Online (Sandbox Code Playgroud)
使用OpenCSV 2.3,这对我来说很好.祝好运!
| 归档时间: |
|
| 查看次数: |
6996 次 |
| 最近记录: |