Tom Tom - 1 year ago 138
Java Question

How to set a default value for boolean field in a model class for Camel Bindy

I'm using Bindy to a map csv file data to a given model class. This whole process works fine, except the default value of the boolean field. This field is not part of the csv file, but necessary in the model class for further processes. I have set the default value to

, but this will be "ignored" and the field is currently always set to
if I run the camel route.

This is a snippet of my model class:

@CsvRecord(separator = " ", skipFirstLine = false)
public class MyModel {

// ... more data fields

@DataField(pos = 8, defaultValue = "true")
public boolean approved;

expects a string value so I can't set it to
. I already tried other values like "TRUE", "yes", "y" and "1" with no luck.

The source of BindyCsvFactory says that the class Format is used to set the default value, but it seems that it can't handle boolean fields and uses the default of
instead and this is

The locale is currently set to "en_Us" using this command:

BindyCsvDataFormat format = new BindyCsvDataFormat("[model package]");

So the main question is: How do I set the default value of a boolean field to
in a Bindy managed model class?

Answer Source

You can't. If you have a look at the FormatFactory code ( you can see that it doesn't support booleans.

It looks like your choices are either

  • Remove the @DataField annotation and just initialise the boolean with public boolean approved = true. You'll have to add it back in later once the approved field is added to the input file.
  • Read the column as a string or integer and convert to a boolean in your code.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download