我正在使用beanio解析固定长度的内容(单字符串行)。我想使用基于注释的配置,但是它不起作用。

以下代码可能有什么问题?

        StreamBuilder builder = new StreamBuilder("s1").addRecord(MyRecord.class);
        StreamFactory factory = StreamFactory.newInstance();
        factory.define(builder);

        Unmarshaller unmarshaller = factory.createUnmarshaller("s1");
        unmarshaller.unmarshal("123ASD");

@Record(minOccurs = 1, maxOccurs = 1)
public class MyRecord {

    @Field(at = 1, length = 3, minOccurs = 1)
    private String number;

    @Field(at = 4, length = 3, minOccurs = 1)
    private String text;
}


org.beanio.InvalidRecordException: Invalid 'myRecord' record
 ==> Invalid 'number':  Expected minimum 1 occurrences
 ==> Invalid 'text':  Expected minimum 1 occurrences
    at org.beanio.internal.parser.UnmarshallingContext.validate(UnmarshallingContext.java:200)
    at org.beanio.internal.parser.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:204)
    at org.beanio.internal.parser.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:89)

最佳答案

我缺少以下内容:

new StreamBuilder("s1").format("fixedlength").addRecord(MyRecord.class);

10-08 03:09