Search code examples
javaspringvalidationspring-integrationspring-integration-dsl

Spring Integration Filter With Exception


I use SpringIntegration-filter for validate my WS message. I implement Validators for validating and if WS message is valid, they returns true. But if WS messages are invalid, they throws a MyValidationException.

Is there a way for handle this exceptions with usage of SpringIntegration-filter? If I don't return false, filter don't work.

My code example is below. I want to use my validation exceptions in discard flow.

@Bean
public IntegrationFlow incomingRequest() {
    return f -> f
        .<IncomingRequest>filter(message ->
            validatorFactory.validator(message.getName())
                .validate(message),
            filterEndpointSpec -> 
                filterEndpointSpec.discardChannel(discardChannel()))
        .<IncomingRequest>handle((payload, headers) ->
            applicationService.handle(payload));
}

@Bean
public IntegrationFlow discard() {
    return IntegrationFlows.from(discardChannel())
        .log("DISCARD FLOW")
        .get();
}

@Bean(name = "discard.input")
public MessageChannel discardChannel() {
    return MessageChannels.direct().get();
}

Solution

  • The discard channel just gets the rejected inbound message; there is no way to alter it in the filter.

    You can do something like this...

    .handle()   // return an Exception on validation failure
    .filter(...) // filter if payload is exception; the exceptions go to the discard channel
    

    i.e. separate the validation and filter concerns.