【发布时间】:2015-12-16 10:08:31
【问题描述】:
BeanIO reference guide 声明对于固定长度的流:
如果 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 个空格被解组为一个空字符串。我错过了什么?如何将空格解组为空字符串?
【问题讨论】: