3

I try to unmarshall csv file using apache camel and bindy. I created a model with some fields annotated like this:

@DataField(pos = 5, defaultValue = "")

The problem is when my csv file contains a column with empty String. Then I got a null value as a result of unmarshalling. I would like to have empty String there as well. How should I write my annotation to get this?

4

2 に答える 2

2

バージョン (2.18.0) がサポートされているようです。

文字列トークン (CSV ファイルから) を @DataField によって注釈が付けられたフィールドに変換する際の手順は次のとおりです。

  1. trim=true の場合、文字列トークンをトリムします
  2. 必要であり、string-token が "" の場合、Exception をスローします
  3. フィールドのタイプに基づいて、適切なコンバーターを取得します
  4. string-token が "" でない場合、string-token でコンバーターを使用します
  5. それ以外の場合、デフォルト文字列が指定されている場合は、デフォルトでコンバーターを使用します
  6. そうでなければ、Java プリミティブ (int など) が適切な値 (MIN) を返す場合
  7. そうでなければnullを返す

バージョン 2.18.0 では、新しいアノテーション @BindyConverter が導入され、Format インターフェースに準拠する任意のクラスを指定できるようになりました (上記のステップ 3 を効果的に傍受します)。

カスタム コンバーターを使用すると、デフォルト値を読み取り、独自の要件に従って変換できます (上記の手順 5)。コンバーターもステップ 4 を処理する必要がありますが、これは文字列の場合は簡単です。

于 2016-07-07T18:00:52.343 に答える