I have a problem with CsvToBeanBuilder from opencsv. I have a class:
public class TransferNetworkRawData {
@CsvBindByName
private String jurisdiction;
@CsvBindByName
private String currency;
@CsvBindByName
private String transfernetwork;
@CsvBindByName
private String display;
public TransferNetworkRawData() {
}
// getters and setters
}
And a service (FYI: TransferNetworkDataFetcher is just an interface with one method getTransferNetworkData(). I am creating this class in such manner, because I want the data to be loaded on application startup without having to read the file every time a request for this data comes.
@Component
public class CsvTransferNetworkDataFetcher implements TransferNetworkDataFetcher {
private static final Logger LOGGER = LoggerFactory.getLogger(CsvTransferNetworkDataFetcher.class);
private static final char SEPARATOR = ';';
private List<TransferNetworkRawData> transferNetworkRawData = new ArrayList<>();
public CsvTransferNetworkDataFetcher(ResourceLoader resourceLoader) {
try {
Resource resource = resourceLoader.getResource("classpath:" + "csv/transferNetworks.csv");
CSVReader reader = new CSVReaderBuilder(new FileReader(resource.getFile())).build();
transferNetworkRawData = new CsvToBeanBuilder(reader)
.withType(TransferNetworkRawData.class)
.withSeparator(SEPARATOR)
.build()
.parse();
} catch (IOException exception) {
LOGGER.error("File not found! " + exception.getMessage());
}
}
@Override
public List<TransferNetworkRawData> getTransferNetworkData() {
return transferNetworkRawData;
}
}
The CSV file looks like this (the file has 28 sets of data + header, so 28 objects in general)
jurisdiction;currency;transfernetwork;display
US;AAVE;ETHEREUM;TRUE
US;BAT;ETHEREUM;TRUE
EU;BCAP;ETHEREUM;TRUE
And a test:
@SpringBootTest
class CsvTransferNetworkDataFetcherTest {
@Autowired
private CsvTransferNetworkDataFetcher transferNetworkDataFetcher;
@Test
void shouldReturnTransferNetworkList() {
List<TransferNetworkRawData> transferNetworkDataList = transferNetworkDataFetcher.getTransferNetworkData();
System.out.println(transferNetworkDataList); // just for breakpoint
}
}
Running this test shows some unexpected results, as getTransferNetworkData() is returning a list of 28 TransferNetworkRawData objects, but all the values (jurisdiction, currency, transfernetwork and display) in all those 28 objects are null, so it seems that the file is loaded correctly but the data isn't correctly read from it...
I have no idea what am I missing here...