apache-camelbindy

How to tell Apache Camel BindyCsvDataFormat to ignore blank lines?


I have a CSV file that ends with a few empty lines. I would like to unmarshal each non-empty line into a Java object using BindyCsvDataFormat as follows:

//within my RouteBuilder class
from("{{route.from}}")
.unmarshal(new BindyCsvDataFormat(MyClass.class).)

@CsvRecord(separator = ",", skipField = true, skipFirstLine = true, generateHeaderColumns = true, allowEmptyStream = true)
@Data
public class MyClass {

    @DataField(pos = 1, required = true)
    @NotEmpty
    private String myString;
    
    @DataField(pos = 2, required = true, pattern = "M/d/yyyy")
    @NotNull
    private LocalDate myDate;

    @DataField(pos = 3, required = false, pattern = "M/d/yyyy")
    private LocalDate notRequiredDate;
}

When Camel attempts to unmarshal an empty line (containing only a carriage return followed by a line feed), it throws:

java.lang.IllegalArgumentException: The mandatory field defined at the position 1 is empty for the line: 5

How do I tell Apache Camel to ignore empty lines?


Solution

  • I don't see that this is possible with Bindy. A potential workaround would be to split the file on line endings, then conditionally unmarshal individual lines through Bindy based on whether or not the line is empty.