I realized that I can get the lineNumber value into MyObject by overriding the DefaultLineMapper with my own LineMapper in this way:
import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import my.model.MyObject;
public class MyLineMapper<T> implements LineMapper<MyObject>, InitializingBean {
private LineTokenizer tokenizer;
private ResourceFieldSetMapper fieldSetMapper;
public MyObject mapLine(String line, int lineNumber) throws Exception {
try{
MyObject r = fieldSetMapper.mapFieldSet(tokenizer.tokenize(line));
// this is the modification
r.setLineNumber(lineNumber);
return r;
}
catch(Exception ex){
throw new FlatFileParseException("Parsing error at line: " + lineNumber +
", input=[" + line + "]", ex, line, lineNumber);
}
}
public void setLineTokenizer(LineTokenizer tokenizer) {
this.tokenizer = tokenizer;
}
public void setFieldSetMapper(ResourceFieldSetMapper fieldSetMapper) {
this.fieldSetMapper = fieldSetMapper;
}
public void afterPropertiesSet() {
Assert.notNull(tokenizer, "The LineTokenizer must be set");
Assert.notNull(fieldSetMapper, "The FieldSetMapper must be set");
}
}
Thanks for your help! I hope this works for someone!
Blessings!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…