What is the best way to do validation in Spring Integration. For example if we have an inbound gateway, when a message is received we want to validate it. If it's not valid -> return the validation errors to the gateway, else -> proceed with the normal flow of the application(transform, handle ...).
I tried a filter:
@Bean
public IntegrationFlow flow() {
return IntegrationFlows.from(requestChannel())
.transform(new MapToObjectTransformer(Campaign.class))
.filter(Campaign.class,
c -> c.getId() > 10 ? true : false, //if id > 10 then it's valid
e -> e.discardChannel(validationError()))
.handle(new MyHandler())
.get();
}
@Bean
public IntegrationFlow validationErrorFlow() {
return IntegrationFlows.from(validationError())
.handle(new ValidationHandler())//construct a message with the validation errors
.get();
}
It works, but that way if I use a spring validator then i have to call it twice, in the filter and in the ValidationHandler (can be a transformer) to get the errors.
Any better way?