Records grouping error in the Camel BeanIO component
Environment
- JBoss Fuse 6.0
Issue
I'm using a Camel BeanIO component [1] to convert the CSV messages into the Java POJO objects. When I send the CSV message to the Camel I see the following error:
Caused by: org.beanio.BeanWriterException: Bean identification failed: no record or group mapping for bean
class 'class com.example.ExamplePojo' at the current position
at org.beanio.internal.parser.BeanWriterImpl.write(BeanWriterImpl.java:81)
at org.beanio.internal.parser.BeanWriterImpl.write(BeanWriterImpl.java:51)
at org.apache.camel.dataformat.beanio.BeanIODataFormat.writeModels(BeanIODataFormat.java:131)
Resolution
This is a known bug in the BeanIO library. The issue has been fixed in BeanIO 2.0.7 and 2.1.0.M2 . Due to the mentioned bug BeanIO ignores minOccurs="0" attribute on the record level and throws BeanWriterException if given record is not specified. The snippet below demonstrates optional record definition in the BeanIO descriptor.
<record name="foo class="com.example.ExamplePojo" minOccurs="0">
...
</record>
One way to handle this issue is to upgrade to the JBoss Fuse 6.1 (which is shipped with the BeanIO in version 2.0.7, not affected by the bug).
The other workaround for this issue is to normalize [1] the message before arrival and add an empty required record to it. The most simple way to achieve a message normalization in Camel would be to create bean [2] normalizing the message, just as demonstrated on the snippet below.
import org.apache.camel.Body;
public class OptionalRecordNormalizer {
public String normalize(@Body String originalBody) {
// Do nothing if there is OptionalRecord present.
if(originalBody.contains("\nRecordBeforeOptionalRecord/")) {
return originalBody;
}
// Add missing OptionalRecord.
return originalBody.replaceAll("(RecordBeforeOptionalRecord/.*)","$1\nOptionalRecord/");
}
}
The normalization bean can be wired into the Camel route as follows:
from("direct:start").
log("Original Message: ${body}").
bean(OptionalRecordNormalizer.class).to(...);
[1] http://camel.apache.org/normalizer.html
[2] http://camel.apache.org/parameter-binding-annotations.html
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
