I want to upload a CSV file from html page and then in the java controller want to convert it to Model Object.
I am using the following approach:
ICsvBeanReader beanReader = null;
try {
beanReader = new CsvBeanReader(new InputStreamReader(file.getInputStream()),
CsvPreference.STANDARD_PREFERENCE);
// the header elements are used to map the values to the bean (names
// must match)
final String[] header = beanReader.getHeader(true);
// get Cell Processor
final CellProcessor[] processors = getProcessors();
CSVData csvData;
while ((csvData = beanReader.read(CSVData.class, header, processors)) != null) {
System.out.println(csvData);
}
} catch (Throwable t) {
t.printStackTrace();
}
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new NotNull(), // CustomerId
new NotNull(), // CustomerName
new NotNull(),
new NotNull(),
new NotNull(),
new NotNull(new ParseInt()),
new NotNull(new ParseInt()),
new NotNull(new ParseInt()),
new NotNull(new ParseDouble()),
new NotNull(),
new NotNull()
};
return processors;
}
This is working for fields having no white spaces like msgID .But it is failing for fields with white spaces like MSG DATA . It is giving error for fields like MSG DATA as follows:
org.supercsv.exception.SuperCsvReflectionException: unable to find method setMSG DATA(java.lang.String) in class com.springboot.dto.CSVData - 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
Thanks