BeanIO 参考指南指出,对于固定长度的流:
如果 required 设置为 false,则无论填充字符如何,都会将空格解组为空字段值。
因此,如果我对这句话的理解正确,则意味着该 pojo 应该通过以下测试:
@Record
public class Pojo {
@Field(length = 5, required = false)
String field;
// constructor, getters, setters
}
考试:
@Test
public void test(){
StreamFactory factory = StreamFactory.newInstance();
factory.define(new StreamBuilder("pojo")
.format("fixedlength")
.addRecord(Pojo.class));
Unmarshaller unmarshaller = factory.createUnmarshaller("pojo");
Pojo pojo = (Pojo) unmarshaller.unmarshal(" "); // 5 spaces
assertNull(pojo.field);
}
但它失败了,这 5 个空格被解组为一个空字符串。我错过了什么?如何将空格解组为空字符串?