本文介紹了Spring Batch FlatFileItemReader繼續使用錯誤數量的令牌的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我使用Spring BatchFlatFileItemReader
解析CSV文件。每隔一段時間,我就會收到格式錯誤的行,應用程序完全崩潰,并顯示:
Caused by: org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 11 actual 18
有沒有辦法告訴FlatFileItemReader
在不完全退出應用程序的情況下繼續(拋出異常并繼續或忽略并繼續)。
我猜我可能需要擴展FlatFileItemReader才能實現這一點,因為似乎沒有對此進行任何設置。對于如何最好地繼續并實現這一點,有什么建議嗎?
推薦答案
我能夠通過創建擴展注入FlatFileItemReader
的DefaultLineMapper
的類來解決此問題。
然后我重寫了mapLine方法,如下所示:
@Override
public T mapLine(String line, int lineNumber) throws Exception {
T t = null;
try {
t = super.mapLine(line, lineNumber);
} catch (Exception e) {
log.error("Unable to parse line number <<{}>> with line <<{}>>.", lineNumber, line);
}
return t;
}
這篇關于Spring Batch FlatFileItemReader繼續使用錯誤數量的令牌的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,