Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

In Spring Batch, what does it mean when an ItemProcessor returns null, and how would you chain validation and transformation processors?

When an ItemProcessor returns null, the item is filtered: it is not passed to the writer, nothing fails, and the step counts it in its filter count. To chain validation and transformation, we use a CompositeItemProcessor that calls its delegates in order, often starting with a ValidatingItemProcessor whose setFilter(true) turns invalid items into filtered ones instead of exceptions. Throwing an exception is the other choice and triggers skip or retry rules, and because a chunk can be processed again, processors should not have side effects outside the transaction.

Why this question is asked

The processor is the part of a Spring Batch step where business rules live: dropping rows we do not want, checking values, and converting one shape into another. How a processor says no to an item matters, because there are two very different ways, and they show up differently in the step's counts and in its failure behaviour. Interviewers ask this to check that we know the null convention and can build a clean chain of small processors. In this answer, we will recap the processor's role, see what returning null does, compare it with throwing, build a validation and transformation chain, and explain why processors must avoid side effects.

1A Quick Recap of the ItemProcessor

In a chunk-oriented step, the reader produces items one at a time, the processor receives each item and returns a result, and the writer receives the results of a whole chunk. The processor's interface has a single method, process(I item), which returns an O, so it can change the type: a CsvRow can become a Customer. A step does not need a processor at all; without one, items go from the reader straight to the writer.

Because the processor sees one item at a time and returns one result, it is the natural place for rules like only active customers or email must be valid. The question is how it signals that an item should not continue.

2What Returning Null Means

Spring Batch treats a null return as a filter decision. The item is dropped from the chunk, so the writer never sees it, and the StepExecution's filter count goes up by one. Nothing is logged as an error, and no skip or retry rule is involved. This is the intended way to say that an item is valid input but not something we want to write.

Java
public class ActiveOnlyProcessor implements ItemProcessor<Customer, Customer> {
@Override
public Customer process(Customer c) {
return c.active() ? c : null; // null = filter this item
}
}
// 1,000 read, 120 inactive
// readCount = 1000, filterCount = 120, writeCount = 880, skipCount = 0

Notice the counts. Read, filtered and written add up, and the skip count stays at zero. That makes the step's results easy to reconcile: we can prove that every input row was either written or deliberately filtered. If filtered items need to be reported, an ItemProcessListener's afterProcess method receives the item and a null result, so we can log or store them.

3Filtering Versus Throwing an Exception

The alternative is to throw an exception from the processor. In a plain step, that fails the step. In a fault-tolerant step, configured with .faultTolerant(), the exception is matched against the skip and retry rules: a skippable exception drops the item and increments the process skip count, up to a skip limit, and calls the SkipListener's onSkipInProcess, while a retryable exception makes Spring Batch try again. After a processor exception the chunk transaction rolls back and the chunk is processed again without the bad item, so the cost is higher than a filter.

Null versus exception from a processor
Aspectreturn nullthrow (fault-tolerant step)
Meaningvalid item we do not wantitem is wrong or processing failed
Counted asfilterCountprocessSkipCount, or step fails
LimitnoneskipLimit
Transactionno rollbackchunk rolls back and is redone
ListenerItemProcessListenerSkipListener.onSkipInProcess

So the choice is about meaning. Inactive customers are a normal part of the data, so we filter them. A row with a date that cannot be parsed is bad data, which we want counted as a skip and limited, because if a thousand rows suddenly fail, something upstream is broken and the job should stop.

4Chaining Validation and Transformation

Real jobs usually need several rules. Rather than one large processor, we write small ones and chain them with CompositeItemProcessor, which passes the output of each delegate to the next. If any delegate returns null, the chain stops there and the item is filtered. For validation, ValidatingItemProcessor wraps a Validator, Spring Batch's own small interface with a validate(item) method that throws ValidationException. By default an invalid item causes that exception; with setFilter(true), the processor returns null instead.

Java
@Bean
public ItemProcessor<CsvRow, Customer> customerProcessor() {
ValidatingItemProcessor<CsvRow> validate = new ValidatingItemProcessor<>(row -> {
if (row.email() == null || !row.email().contains("@")) {
throw new ValidationException("bad email in row " + row.lineNumber());
}
});
validate.setFilter(true); // invalid -> filtered, not an error
ItemProcessor<CsvRow, CsvRow> activeOnly = r -> r.active() ? r : null;
ItemProcessor<CsvRow, Customer> toCustomer =
r -> new Customer(r.id(), r.name().trim(), r.email().toLowerCase());
return new CompositeItemProcessorBuilder<CsvRow, Customer>()
.delegates(validate, activeOnly, toCustomer) // called in this order
.build();
}
// row with bad email -> filtered at step 1
// inactive row -> filtered at step 2
// good, active row -> Customer written
Step throughOne row through the composite processor
  1. Is the row valid?ValidatingItemProcessor, setFilter(true)
    YesCheck if activeNoReturn null: filtered
  2. Is the customer active?
    YesTransform to CustomerNoReturn null: filtered
  3. Transform to Customertrim, lower-case
  4. Add to chunk for the writer

4 steps in order. Play them, or step through them one at a time.

The order matters. Validation comes first, so later processors can rely on clean values. Cheap filters come before expensive transformations, so we do not convert rows we will drop. For bean validation annotations such as @NotNull and @Email, Spring Batch also offers BeanValidatingItemProcessor, which validates with Jakarta Bean Validation.

5Why Processors Must Avoid Side Effects

A chunk can be processed more than once. If the writer fails in a fault-tolerant step, the chunk is rolled back and its items may be processed again, one at a time, while Spring Batch looks for the failing item. On a restart after a failure, the last uncommitted chunk is read and processed again. So any effect a processor has outside the chunk's transaction may happen twice: an email sent, a call to another service, an increment in a cache.

The safe rule is that a processor should be a pure function of its input, or at most read reference data. Effects that must happen belong in the writer, inside the transaction, or in a later step built to be idempotent, such as sending from an outbox table.

6Follow-up questions

Does a filtered item count against the skip limit?

No. Filtering and skipping are separate. Filtered items increase filterCount and have no limit, while skips increase the skip counts and are limited by skipLimit. That is why we filter expected cases and skip only real errors.

Can a processor return a different object type?

Yes. The processor's signature is ItemProcessor<I, O>, so it can turn a CsvRow into a Customer. In a composite, each delegate's output type must match the next delegate's input type, and the composite's overall types are the first input and the last output.

Should validation be in the processor or the reader?

Parsing errors, such as a line with the wrong number of fields, appear in the reader and can be skipped there. Business validation, such as a required email or a date in the past, belongs in the processor, where it sees a typed item and where the decision to filter or fail is explicit.

7Conclusion

In this answer, we saw that returning null from an ItemProcessor filters the item: the writer never sees it and the step counts it as filtered. Throwing instead triggers skip or retry rules in a fault-tolerant step and rolls back the chunk. We chained small processors with CompositeItemProcessor, starting with a ValidatingItemProcessor whose setFilter(true) turns invalid items into filtered ones, and we explained why processors must not have side effects outside the transaction, because chunks can be processed again.

What to say in the interviewAbout 90 seconds aloud

Open with

Returning null from an ItemProcessor means the item is filtered. It is not passed to the writer, nothing is treated as an error, and the StepExecution's filter count goes up, so read count equals filtered plus written plus skipped and we can reconcile the run.

Then, if they want more

I use it for valid data we simply do not want, like inactive customers. Throwing an exception is different: in a fault-tolerant step it goes through the skip and retry policies, counts as a process skip up to the skip limit, and rolls back the chunk, so I use that for bad data where I want a limit and visibility.

To chain rules I keep processors small and combine them with CompositeItemProcessor, which calls the delegates in order and stops as soon as one returns null. Usually the first delegate is a ValidatingItemProcessor with a Validator; by default an invalid item throws ValidationException, and with setFilter(true) it is filtered instead.

Then come cheap filters, then the transformation to the output type. One rule I always follow is that processors have no side effects outside the transaction, like sending emails or calling other services, because a chunk can be processed again during a skip scan or after a restart, so those effects would happen twice. Those belong in the writer or in a separate idempotent step.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Batch reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Spring Batch, what does it mean when an ItemProcessor returns null, and how would you...": https://scrivio-interview-library.netlify.app/spring-boot/spring-batch/570e7d0427/