Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

and using this to convert it to a list of IDs

val recommendationUpdateList = CSVUtils.convertToBean(file, BeanClass::class.java)

CSVUtils.convertToBean looks like this :

public static <T> List<T> convertToBean(File file, Class<T> bean) throws IOException {
        List<T> csvData = null;
        try (Reader reader = new FileReader(file)) {
            HeaderColumnNameMappingStrategy<T> strategy =
                    new HeaderColumnNameMappingStrategy<T>();
            strategy.setType(bean);
            CsvToBean<T> cb = new CsvToBeanBuilder<T>(reader)
                    .withType(bean)
                    .withFieldAsNull(CSVReaderNullFieldIndicator.BOTH)
                    .withMappingStrategy(strategy)
                    .build();
            csvData = cb.parse();
        return csvData;

Now when I upload a CSV like this, it's working fine

HOWEVER, I want it throw an error when I use the following CSV (notice the name of CSV header has changed)

Please suggest a way ahead where header of csv can be validated against the bean column headers. What it is doing currently is picking default value -1, also in my usecase I can't verify on basis of default value.

Does adding required does the work:

data class BeanClass (
    @CsvBindByName(column = "Id", required = true)
    val id: Long = -1

It will throw CsvRequiredFieldEmptyException

Found a simple solution :

public static <T> List<T> convertToBean(File file, Class<T> bean, Boolean validate) throws IOException, CsvRequiredFieldEmptyException, RequestInvalidException {
        List<T> csvData = null;
        try(Reader reader = new FileReader(file)) {
            HeaderColumnNameMappingStrategy<T> strategy = new HeaderColumnNameMappingStrategy<T>();
            strategy.setType(bean);
            CsvToBean<T> cb = new CsvToBeanBuilder<T>(reader)
                    .withType(bean)
                    .withFieldAsNull(CSVReaderNullFieldIndicator.BOTH)
                    .withMappingStrategy(strategy)
                    .build();
            csvData = cb.parse();
            if(validate) {
                String[] validHeaders = getHeaders(bean);
                Set<String> validHeaderSet = new HashSet<>(Arrays.asList(validHeaders));
                String[] actualHeaders = strategy.generateHeader((T) bean);
                for(String header : actualHeaders) {
                    if(!validHeaderSet.contains(header.toUpperCase())) {
                        throw new Exception();
        return csvData;
    public static <T> String[] getHeaders(Class<T> bean) throws CsvRequiredFieldEmptyException {
        HeaderColumnNameMappingStrategy<T> strategy = new HeaderColumnNameMappingStrategy<T>();
        strategy.setType(bean);
        return strategy.generateHeader((T) bean);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.