2

I have the following route:

from("direct:abc")
        // read the file  
        .split(body().tokenize("\n", 3, false)).streaming().stopOnException()
        .unmarshal(new BindyCsvDataFormat(Foo.class))
        .process(new FooListProcessor());

The problem is that if I have 4 records in file first group comes in processor as List but the second as a single Foo object. I have to check body with instanceof and create a list every time when such cases occurs.

Foo class:

@CsvRecord(separator = ",")
public class Foo {
   @DataField(pos = 1)
   private String fooField;
   @DataField(pos = 2, trim = true)
   private String barField;
}

File content:

"lorem","ipsum"
"dolorem","sit"
"consectetur","adipiscing"
"eiusmod","incididunt"

Is there a way to force Bindy to always unmarshall into a List?

A5300
  • 409
  • 4
  • 18

2 Answers2

2

No bindy returns a single instance if there is single instance. And a list of there are more.

I have logged a ticket for an improvement so you can configure this: https://issues.apache.org/jira/browse/CAMEL-12321

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
1

Just a small point. Since it is not supported as @Claus said, instead of you doing instance of check in processor code, you could as well do it in the route like this and let camel handle it for you.

  from("file:///tmp/camel/input")
            // read the file
            .split(body().tokenize("\n", 3, false)).streaming().stopOnException()
            .unmarshal(new BindyCsvDataFormat(Foo.class))
            .choice()
            .when(body().isInstanceOf(List.class))
                .process(exchange -> { // Your logic here for list})
            .otherwise()
                .process(exchange -> {// Your logic here for individual items})
            .endChoice();
pvpkiran
  • 25,582
  • 8
  • 87
  • 134