BeanIO

时间:2018-02-16 17:22:50

标签: utf-8 apache-camel unmarshalling bean-io fixed-length-record

我对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>

类实现是这样的:

    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类的实现,所以前面的如预期的那样,字段保持未初始化,但在编组后正确打印出来。我哪里出错了?

好的,我的骆驼代码是在spring xml中,看起来像这样:

    <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}\0</simple>
                </transform>
        <unmarshal ref="parseTransactions464"/>
        <marshal ref="jack"/>
        <log id="route-log-json" message="jackson: ${body}"/>
</route>

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

2 个答案:

答案 0 :(得分:0)

我将尝试这个作为答案,不幸的是我无法测试这个,我没有任何设置可用于Camel。首先,我不会更改recordTerminator的默认BeanIO值,并让其使用默认值CRLFCRLF

然后在转换消息时,我会附加换行符(\ n)而不是\0。我不太明白为什么你想要使用EOF字符,如果你能控制它。而不是:

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

我会选择:

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

请参阅页面底部附近的section on "Using New Lines or Tabs in XML DSLs"

  

在XML DSL中使用新行或标签

     

可从Camel 2.9.3

获得      

从Camel 2.9.3:在XML中指定新行或制表符更容易   DSL你可以逃脱现在的值xml

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

答案 1 :(得分:0)

我一直在试图找出问题,但最后,我意识到我应该使用beanio dataFormat的编码属性设置charset,由于这个缺陷我无法做到:

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

最后,克劳斯易卜生指示我使用这种解决方法:

    <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>