Java 使用BeanIO将空白字符串解组为null
表示对于固定长度的流: 如果将required设置为false,则无论填充字符是什么,空格都将解组为空字段值 因此,如果我正确理解这句话,这意味着以下测试应该通过此pojo:Java 使用BeanIO将空白字符串解组为null,java,bean-io,Java,Bean Io,表示对于固定长度的流: 如果将required设置为false,则无论填充字符是什么,空格都将解组为空字段值 因此,如果我正确理解这句话,这意味着以下测试应该通过此pojo: @Record public class Pojo { @Field(length = 5, required = false) String field; // constructor, getters, setters } 测试: @Test public void test(){
@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个空格被解组为空字符串。我错过了什么?如何将空格解组为空字符串?最后,我使用基于以下内容的 更新: 作为上的用户,还可以在
@字段上使用trim=true,lazy=true
:
@Field(length = 5, trim = true, lazy = true)
String field;
trim=true lazy=true执行作业+1
@Field(length = 5, trim = true, lazy = true)
String field;