Tom Tom - 4 months ago 22
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

true
, but this will be "ignored" and the field is currently always set to
false
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;


defaultValue
expects a string value so I can't set it to
Boolean.TRUE
. 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
boolean
instead and this is
false
.

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

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


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

Answer

You can't. If you have a look at the FormatFactory code (http://camel.apache.org/maven/camel-2.11.0/camel-bindy/apidocs/src-html/org/apache/camel/dataformat/bindy/FormatFactory.html) 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.
Comments