18.3. 使用方法
マッピングファイルの例はこちら にあります。
BeanIODataFormat を使用するには、マッピングファイルとストリームの名前を使用してデータ形式を設定する必要があります。
その場合は以下を実行します。streamName は employeeFile です。
Java
XML
DataFormat format = new BeanIODataFormat(
"org/apache/camel/dataformat/beanio/mappings.xml",
"employeeFile");
// a route which uses the bean io data format to format the CSV data
// to java objects
from("direct:unmarshal")
.unmarshal(format)
// and then split the message body, so we get a message for each row
.split(body())
.to("mock:beanio-unmarshal");
// convert a list of java objects back to flat format
from("direct:marshal")
.marshal(format)
.to("mock:beanio-marshal");
<route>
<from uri="direct:unmarshal"/>
<unmarshal>
<beanio mapping="org/apache/camel/dataformat/beanio/mappings.xml" streamName="employeeFile"/>
</unmarshal>
<split>
<simple>${body}</simple>
<to uri="mock:beanio-unmarshal"/>
</split>
</route>
<route>
<from uri="direct:marshal"/>
<marshal>
<beanio mapping="org/apache/camel/dataformat/beanio/mappings.xml" streamName="employeeFile"/>
</marshal>
<to uri="mock:beanio-marshal"/>
</route>
XML で BeanIO データ形式を使用するには、以下に示すように <beanio> XML タグを使用して設定する必要があります。ルートは上記の例と同じようになります。
最初のルートは、CSV データを List<Employee> Java オブジェクトに変換するためのものです。次にこれを分割して、モックエンドポイントが行ごとにメッセージを受け取るようにします。
2 番目のルートは逆の操作を実行するためのもので、List<Employee> を CSV データのストリームに変換します。
CSV データは、たとえば次のようになります。
Joe,Smith,Developer,75000,10012009
Jane,Doe,Architect,80000,01152008
Jon,Anderson,Manager,85000,03182007