0

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?

James
  • 2,876
  • 18
  • 72
  • 116

2 Answers2

2

A possible alternative could be to filter out empty lines before unmarshalling. This can be easily done using the filter EIP and simple language:

from("direct:demo")
    .split(body().tokenize("\n"))
    .streaming()
    .filter( simple("${body.length} > 0") ) 
    .unmarshal(new BindyCsvDataFormat(MyClass.class))
TacheDeChoco
  • 3,683
  • 1
  • 14
  • 17
1

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.

Jeremy Ross
  • 11,544
  • 3
  • 36
  • 36