Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

spring - How to handle exception and skip the wrong csv line as well?

I am reading a csv file using Spring batch. I am reading the content of csv and writing this content to the database in accordance with one of the entity class.

Now there could be certain lines in csv that are wrong and don't match the POJO attributes. To handle this I configured my Step as follows:

Step step = stepBuilderFactory.get("CSV-Step")
                .<Book, Book>chunk(100)
                .faultTolerant()
                .skip(FlatFileParseException.class)
                .skipLimit(1)
                .reader(itemReader)
                .writer(itemWriter)
                .build();

It basically skips the line which causes FlatFileParseException and goes on with subsequent lines. Now I also want to log the lines for which parsing could not be done. For this in my GlobalExceptionHandler annotated with @ControllerAdvice I made following method:

@OnReadError
public void handleCsvParseException(FlatFileParseException ex, Throwable throwable) {
    logger.error("! FlatFileParseException, line  is: " + ex.getLineNumber());
    logger.error("! FlatFileParseException, input is: " + ex.getInput());
    logger.error("! Message: " + throwable.getMessage());
    logger.error("! Cause: " + throwable.getCause());
}

The thing is that this method is not being called because i have the skip configuration in my Step. How can i ignore the unwanted lines i.e. skipping unwanted line but at the same time log information about them. I would appreciate any kind of help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The SkipListener is what you need. This listener will be called whenever the configured skippable exception occurs during reading, processing or writing items.

In your case, you can implement the logging logic in the SkipListener#onSkipInRead(Throwable t); method. The FlatFileParseException will be passed as a parameter and will give you the necessary context (the line number and the raw input).

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...