Announcement Announcement Module
Collapse
No announcement yet.
Spring Batch Validation Framework - Looking for good Patterns Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Batch Validation Framework - Looking for good Patterns

    Hi,

    I am reading data from CSV flat file using FlatFileItemReader and want to validate the input records and if any errors, I just want to log it and still continue with the other records in the file. Having said that, what would be the good pattern to include the validation layer.

    1) Include validation (custom) layer in InputFeedFieldSetMapper while reading the input from flat file and log the invalid records

    Code:
    	
    	<bean name="feedMapper" class="com.abc.batch.core.reader.mapper.InputFeedFieldSetMapper"/>
    
    <bean name="feedLineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
             <property name="fieldSetMapper" ref="feedMapper" />
             <property name="lineTokenizer" ref="feedLineTokenizer" />
    	</bean>
    2) Include validation in Processing layer as shown below and use Errors.rejectValue etc. to log it in log file but I don't want to reject and just want to log it (no idea how to do it and more over if I use CompositeItemProcessor pattern, the subsequent processors won't be called if any Errors out of ValidatingItemProcessor)

    Code:
    	<bean id="feedValidator" class="com.abc.batch.core.reader.validator.impl.FileLoadValidator" />
    
        <bean id="validator" class="org.springframework.batch.item.validator.SpringValidator">
    	  <property name="validator" ref="feedValidator" />
    	</bean>
    
    			<bean id="validateFileLoadProcessor" class="org.springframework.batch.item.validator.ValidatingItemProcessor">
    			  <property name="validator" ref="validator" />	  
    			</bean>
    Please help me with this. Thanks.

  • #2
    When you say you don't want to "reject" it, just log it, what do you mean? Do you still want it to go through the processors and be written? If that's the case, using option 2 would be your best bet. In your Validator implementation, just log the failing item and don't throw the ValidationException.

    Comment

    Working...
    X