Reflection-based mapping with spaces in column name not working
A fast, programmer-friendly, free CSV library for Java
Brought to you by:
jamesbassett,
kbg
Finding a property based on columnname doesn't work when there is a space in the column name.
Lets say I have a column name "My Columnname", the ReflectionUtils of SuperCSV will look for a setter named "setMy Columnname", which is not possible in the java I am using.
org.supercsv.exception.SuperCsvReflectionException: unable to find method setMy Columnname(java.lang.String) in class com.example.SomeClass - check that the corresponding nameMapping element matches the field name in the bean, and the cell processor returns a type compatible with the field context=null at org.supercsv.util.ReflectionUtils.findSetter(ReflectionUtils.java:185) at org.supercsv.util.MethodCache.getSetMethod(MethodCache.java:96) at org.supercsv.io.CsvBeanReader.populateBean(CsvBeanReader.java:154) at org.supercsv.io.CsvBeanReader.readIntoBean(CsvBeanReader.java:264) at org.supercsv.io.CsvBeanReader.read(CsvBeanReader.java:173)
Please trim spaces so I can use a method named "setMyColumnname".
Hi,
Column names (i.e. headers) are totally independent of the field mapping used by
CsvBeanReader
. You don't have to use the same array for the header and for the field mapping passed toCsvBeanReader.read()
. Most of the time you can, but if your headers don't match the bean's field names (as in your example), then you have to use different arrays.Please see this example (while it's using
CsvBeanWriter
, the same principal applies).Your code should look something like this:
I don't like manually configuring my header-names in the code. They are already hard-coded in the CSV files (out of my control) and in my POJO's and I don't feel like maintaining a three-double administration.
I am currently using the following snippet, which formats the column-names to something java should accept.
now, this is only tested for my specific csv files, so there are probably plenty of other characters that the
formatHeaders()
method should filter. I think theReflectionUtils
-class of SuperCSV should be able to handle this on its own