BeanIO 中的段终止符

segment terminator in BeanIO

我对 BeanIO 的 recordTerminator 解析器 属性 很感兴趣。它是否也适用于段,例如 "segmentTerminator"?也就是说,我有一个固定长度格式的流,包含一个具有可重复段的记录,并且所有流都是一行。因此,我设置了 recordTerminator="",但它仍然给我

==> Invalid 'state':  Expected minimum 1 occurrences
==> Invalid 'city':  Expected minimum 1 occurrences
==> Invalid 'street':  Invalid field length, expected 35 characters
==> Invalid 'zip':  Expected minimum 1 occurrences

它不会抱怨可重复段之前的字段,并且抱怨可重复段中的字段在 mapping.xml 中定义的顺序不正确,看起来像这样:

    <beanio  xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
      <stream name="employeeFile" format="fixedlength">
        <parser>
            <property name="recordTerminator" value="" />
        </parser>  
        <record name="employee" class="example.Employee">
          <field name="firstName" length="35" />
          <field name="lastName" length="35" />
          <field name="title" length="35" />
          <field name="salary" length="35" />
          <segment name="addressList" collection="list" minOccurs="1" maxOccurs="unbounded" class="example.Address">
            <field name="street" length="35" />
            <field name="city" length="35" />
            <field name="state" length="35" />      
            <field name="zip" length="10" />
          </segment>
        </record> 
      </stream>
    </beanio>

Class 实现是这样的:

    package example;        
    public class Employee {
        String firstName;
        String lastName;
        String title;
        String salary;
        List<Address> addressList;

        // getters and setters not shown...
    }       

    package example;
    public class Address {
        private String street;
        private String city;
        private String state;
        private String zip;

        // getters and setters not shown...
    }       

如果我从 mapping.xml 和输入字符串中删除所有前面的字段到重复段,剩余的字符串将被正确解组,然后编组到 json,我什至没有改变实现java 类,因此前面的字段如预期的那样保持未初始化状态,但在编组后正确打印出来。我哪里错了?

OK,我的骆驼代码在springxml,看起来像这样:

    <route id="simple-route">
        <!-- from id="request-file" uri="file://C:/mqdocuments/?fileName=response464.txt"/-->
        <from id="request-file" uri="file://C:/mqdocuments/?fileName=request464.txt"/>
        <log id="route-log-request" message="request: ${body}"/>
        <setHeader headerName="CamelJmsDestinationName" id="_setHeader1">
            <constant>queue://QM_TEST/INPUTQ?targetClient=1</constant>
        </setHeader>
        <to id="_to1" pattern="InOut" uri="websphere:queue:SYSTEM.DEFAULT.LOCAL.QUEUE?useMessageIDAsCorrelationID=true&amp;replyTo=REPLYQ"/>
        <log id="route-log-response" message="response: ${body}"/>
                    <transform>
                        <simple>${body}[=13=]</simple>
                </transform>
        <unmarshal ref="parseTransactions464"/>
        <marshal ref="jack"/>
        <log id="route-log-json" message="jackson: ${body}"/>
</route>

所以基本上,当我取消注释文件中的输入,其中保存了响应,并将注释 mq 放在端点中时,解组是可以的,但是如果我将请求放入队列并获得响应,那么我希望通过简单地添加 EOF 字符的转换来纠正问题,因为没有它,它会给我一开始报告的错误。 转换也无济于事,因为我不知道如何编写 EOF (ascii 26),但即使我弄清楚了,我也不确定它是否有帮助。

我将尝试将此作为答案,不幸的是我无法对此进行测试,我没有任何与 Camel 一起使用的设置。首先,我不会更改 BeanIO 的默认 recordTerminator 值,而是使用默认值 CRLFCRLF

然后在消息的转换中,我将附加一个换行符 (\n) 而不是 [=18=]。我不太明白为什么要使用 EOF 字符(如果可以控制的话)。而不是:

<transform>
  <simple>${body}[=10=]</simple>
</transform>

我会选择:

<transform>
  <simple>${body}\n</simple>
</transform>

查看页面底部附近的section on "Using New Lines or Tabs in XML DSLs"

Using New Lines or Tabs in XML DSLs

Available from Camel 2.9.3

From Camel 2.9.3: it is easier to specify new lines or tabs in XML DSLs as you can escape the value now xml

<transform> <simple>The following text\nis on a new line</simple></transform>

我四处游荡试图找出问题所在,但最后,我意识到我应该用 beanio dataFormat 的编码属性设置字符集,但由于这个缺陷我无法做到:

http://camel.465427.n5.nabble.com/Re-Exhausted-after-delivery-attempt-1-caught-java-lang-NullPointerException-charset-tc5817807.html

http://camel.465427.n5.nabble.com/Exhausted-after-delivery-attempt-1-caught-java-lang-NullPointerException-charset-tc5817815.html

https://issues.apache.org/jira/browse/CAMEL-12284

最后,Claus Ibsen 指示我使用这样的解决方法:

    <bean class="org.apache.camel.dataformat.beanio.BeanIODataFormat" 
          id="some_bean_id"> 
        <property name="encoding" value="UTF-8"/> 
        <property name="mapping" value="mapping.xml"/> 
        <property name="streamName" value="some_stream_name"/> 
    </bean>