Understanding ItemProcessor in Spring Batch with Practical Example
📌 Note: This blog is a continuation of our previous guide on Importing CSV to Database using Spring Batch. We recommend checking that out first if you're new to Spring Batch basics.
💡 Also Note: While this post uses email validation as a practical example, the ItemProcessor can be used for any kind of data transformation or filtering logic.
🔍 What is ItemProcessor?
In Spring Batch, ItemProcessor is an interface that allows you to perform data transformation or validation logic between reading and writing items. It is typically used for:
- Filtering out invalid data
- Transforming fields or format
- Enriching data from other sources
🛠️ Adding Email Validation Using ItemProcessor
We’ll extend our existing Spring Batch job to add a processor that validates email addresses. Invalid records will be skipped.
1️⃣ Create the EmailValidationProcessor
@Component
public class EmailValidationProcessor implements ItemProcessor<Employee, Employee> {
@Override
public Employee process(Employee employee) throws Exception {
if (employee.getEmail() != null && employee.getEmail().matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$")) {
return employee;
}
return null; // Skip invalid records
}
}
2️⃣ Update SpringBatchConfig to Use Processor
@Configuration
public class SpringBatchConfig {
@Autowired
private JobRepository repository;
@Autowired
private PlatformTransactionManager platformTransactionManager;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired
private EmailValidationProcessor processor;
@Bean
public Job csvToDBJob() {
return new JobBuilder("csv-to-db-job", repository)
.incrementer(new RunIdIncrementer())
.start(cvsToDBStep())
.build();
}
@Bean
public Step cvsToDBStep() {
return new StepBuilder("csv-to-db", repository)
.<Employee, Employee>chunk(5, platformTransactionManager)
.reader(flatFileItemReader())
.processor(processor)
.writer(jpaItemWriter())
.build();
}
@Bean
public JpaItemWriter<Employee> jpaItemWriter() {
JpaItemWriter<Employee> writer = new JpaItemWriter<>();
writer.setEntityManagerFactory(entityManagerFactory);
return writer;
}
@Bean
public FlatFileItemReader<Employee> flatFileItemReader() {
return new FlatFileItemReaderBuilder<Employee>()
.name("flat-file-item-reader-1")
.linesToSkip(1)
.resource(new FileSystemResource("E://exported-files//employee.csv"))
.delimited()
.names("name", "email")
.targetType(Employee.class)
.build();
}
}
3️⃣ Reuse Existing Controller
Use the same REST endpoint from our previous blog to trigger the batch job:
GET http://localhost:8080/job-launcher/launch-csv-to-db-job
✅ Summary
With just a small change, we've enhanced our Spring Batch job to validate data before writing it to the database. You can use this same technique to filter or transform data for other business rules.
- 🧠 Understand what ItemProcessor is
- ✅ Implement logic to validate emails
- 🛠️ Extend batch jobs with minimal changes
This flexibility makes Spring Batch a powerful choice for any data-processing pipeline.
🔗 Related Posts
🔧 Spring Batch Core Components
Understand the fundamental building blocks: ItemReader, ItemProcessor, and ItemWriter.
📥 Import CSV to Database
Build a real Spring Batch job that reads CSV, processes records, and writes to the database.
🚫 Skip Policy & Error Handling
Learn how to handle processing errors without failing the entire job.
🔁 Conditional Flow in Jobs
Configure conditional transitions between steps based on exit statuses.