BeanIO 写入 0 而不是预期值

BeanIO writes 0 instead of intended value

我有一个包含记录计数器的固定长度流

以下 BeanIO 映射代码

 <record name="RECORD_Z" class="com.acme.ftt2017.RecordZ" order="4" minOccurs="1" maxOccurs="1" maxLength="1900">
        <field name="tipoRecord" rid="true" at="0" ignore="true" required="true" length="1" lazy="true" literal="Z" />

        <field name="numeroRecordB" at="15" length="9" padding="0" align="right" trim="true" />
        <field name="numeroRecordC" at="24" length="9" padding="0" align="right" trim="true" />

        <field name="terminatorA" at="1897" length="1" rid="true" literal="A" ignore="true" />
    </record>

豆子

public class RecordZ implements Serializable
{    
    private final char tipoRecord = 'Z';

    private Integer numeroRecordB, numeroRecordC;

    // G & S omitted
}

我在调试以下代码时进行了三重检查:

        RecordZ trailer = new RecordZ();
        trailer.setNumeroRecordB(1);
        trailer.setNumeroRecordC(countRecordC); // equals 1 in debug

        log.debug("Exporting record Z");
        log.trace("Record Z: " + trailer.toString());
        exporter.write(FttRecordTypes.RECORDTYPE_FTT_Z, trailer);

但是生成的数据文件包含以下内容

Z              000000000000000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        A

预计

Z              000000001000000001                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        A

我的导出代码有什么问题?为什么我总是得到零?

来自 Section 4.3.1 中的最后一段

Optionally, a format attribute can be used to pass a decimal format for java.lang.Number types, and for passing a date format for java.util.Date types. In the example below, the hireDate field uses the SimpleDateFormat pattern "yyyy-MM-dd", and the salary field uses the DecimalFormat pattern "#,##0". For more information about supported patterns, please reference the API documentation for Java's java.text.DecimalFormat and java.text.SimpleDateFormat classes.

并在 Section 4.3.2

A type handler may be explicitly named using the name attribute, and/or registered for all fields of a particular type by setting the type attribute. The type attribute can be set to the fully qualified class name or type alias of the class supported by the type handler. To reference a named type handler, use the typeHandler field attribute when configuring the field.

因此,您可以尝试以下两种方法之一:

删除自定义 *IntegerTypeHandlers 的使用,并在使用这些自定义类型处理程序的字段上指定 format 属性。这可能需要大量工作,具体取决于您拥有的字段数量和类型处理程序。例如:

<field name="numeroRecordB" format="000000000" at="15" length="9" padding="0" align="right" trim="true" />

在您的自定义类型处理程序中使用 getType() 方法 return null 而不是 Integer.class ,希望这不会被用作全局类型处理程序。我以前没有这样做过,所以它可能行不通。

 public Class<?> getType() {
   return null;
 }

希望对您有所帮助。

发现罪魁祸首。自定义类型处理程序!!!!

根据BeanIO

A type handler may be explicitly named using the name attribute, and/or registered for all fields of a particular type by setting the type attribute. The type attribute can be set to the fully qualified class name or type alias of the class supported by the type handler. To reference a named type handler, use the typeHandler field attribute when configuring the field.

所以我没有在我的文件标题中显示我已经注册了 plenties 个自定义类型处理程序,不幸的是它们都具有 type 属性。

<typeHandler name="int_2" type="java.lang.Integer" class="org.beanio.types.IntFixedLengthTypeHandler">
    <property name="numberOfDigits" value="2" />
</typeHandler>
<typeHandler name="int_4" type="java.lang.Integer" class="org.beanio.types.IntFixedLengthTypeHandler">
    <property name="numberOfDigits" value="2" />
</typeHandler>
<typeHandler name="int_10" type="java.lang.Integer" class="org.beanio.types.IntFixedLengthTypeHandler">
    <property name="numberOfDigits" value="10" />
</typeHandler>

<typeHandler name="bigint_16" type="java.math.BigInteger" class="org.beanio.types.BigIntFixedLengthTypeHandler">
    <property name="numberOfDigits" value="16" />
</typeHandler>

删除 type 属性有效