1

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...

1 Answer 1

1

I'm facing with exactly the same issue in my source code. I just change this annotation @CsvBindByName for @CsvBindByPosition(position = #)

Your new model should be:

public class TransferNetworkRawData {
    @CsvBindByPosition(position = 0)
    private String jurisdiction;
    @CsvBindByPosition(position = 1)
    private String currency;
    @CsvBindByPosition(position = 2)
    private String transfernetwork;
    @CsvBindByPosition(position = 3)
    private String display;

    public TransferNetworkRawData() {
    }
    
    // getters and setters

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.