Chapter 18. BeanIO
Since Camel 2.10
The BeanIO Data Format uses BeanIO to handle flat payloads (such as XML, CSV, delimited, or fixed length formats).
BeanIO is configured using a mapping XML file where you define the mapping from the flat format to Objects (POJOs). This mapping file is mandatory to use.
18.1. Dependencies Copy linkLink copied to clipboard!
When using beanio with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-beanio-starter</artifactId> </dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-beanio-starter</artifactId>
</dependency>
18.2. Options Copy linkLink copied to clipboard!
The BeanIO dataformat supports 8 options, which are listed below.
| Name | Default | Java Type | Description |
|---|---|---|---|
| mapping (common) |
| Required The BeanIO mapping file. Is by default loaded from the classpath. You can prefix with file:, http:, or classpath: to denote from where to load the mapping file. | |
| streamName (common) |
| Required The name of the stream to use. | |
| ignoreUnidentifiedRecords (common) |
|
| Whether to ignore unidentified records. |
| ignoreUnexpectedRecords (common) |
|
| Whether to ignore unexpected records. |
| ignoreInvalidRecords (common) |
|
| Whether to ignore invalid records. |
| encoding (advanced) |
| The charset to use. Is by default the JVM platform default charset. | |
| beanReaderErrorHandlerType (advanced) |
| To use a custom org.apache.camel.dataformat.beanio.BeanIOErrorHandler as error handler while parsing. Configure the fully qualified class name of the error handler. Notice the options ignoreUnidentifiedRecords, ignoreUnexpectedRecords, and ignoreInvalidRecords may not be in use when you use a custom error handler. | |
| unmarshalSingleObject (advanced) |
|
| This options controls whether to unmarshal as a list of objects or as a single object only. The former is the default mode, and the latter is only intended in special use-cases where beanio maps the Camel message to a single POJO bean. |
18.3. Usage Copy linkLink copied to clipboard!
An example of a mapping file is here.
To use the BeanIODataFormat you need to configure the data format with the mapping file, as well the name of the stream.
This can be done as shown below. The streamName is employeeFile.
Java
XML
To use the BeanIO data format in XML, you need to configure it using the <beanio> XML tag as shown below. The routes are similar to the example above.
The first route is for transforming CSV data into a List<Employee> Java objects. Which we then split, so the mock endpoint receives a message for each row.
The second route is for the reverse operation, to transform a List<Employee> into a stream of CSV data.
The CSV data could, for example, be as below:
Joe,Smith,Developer,75000,10012009 Jane,Doe,Architect,80000,01152008 Jon,Anderson,Manager,85000,03182007
Joe,Smith,Developer,75000,10012009
Jane,Doe,Architect,80000,01152008
Jon,Anderson,Manager,85000,03182007
18.4. Spring Boot Auto-Configuration Copy linkLink copied to clipboard!
The component supports 9 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
| camel.dataformat.beanio.bean-reader-error-handler-type | To use a custom org.apache.camel.dataformat.beanio.BeanIOErrorHandler as error handler while parsing. Configure the fully qualified class name of the error handler. Notice the options ignoreUnidentifiedRecords, ignoreUnexpectedRecords, and ignoreInvalidRecords may not be in use when you use a custom error handler. | String | |
| camel.dataformat.beanio.enabled | Whether to enable auto configuration of the beanio data format. This is enabled by default. | Boolean | |
| camel.dataformat.beanio.encoding | The charset to use. Is by default the JVM platform default charset. | String | |
| camel.dataformat.beanio.ignore-invalid-records | Whether to ignore invalid records. | false | Boolean |
| camel.dataformat.beanio.ignore-unexpected-records | Whether to ignore unexpected records. | false | Boolean |
| camel.dataformat.beanio.ignore-unidentified-records | Whether to ignore unidentified records. | false | Boolean |
| camel.dataformat.beanio.mapping | The BeanIO mapping file. Is by default loaded from the classpath. You can prefix with file:, http:, or classpath: to denote from where to load the mapping file. | String | |
| camel.dataformat.beanio.stream-name | The name of the stream to use. | String | |
| camel.dataformat.beanio.unmarshal-single-object | This option controls whether to unmarshal as a list of objects or as a single object only. The former is the default mode, and the latter is only intended in special use-cases where beanio maps the Camel message to a single POJO bean. | false | Boolean |