Search code examples
spring-integrationspring-integration-dsl

Spring Integration Java DSL: How to route with the channelMapping method to the channel which name is in the headers?


How to route with the channelMapping method to the channel which name is in the headers? So if I try this

    @Bean
    private IntegrationFlow postDataToChannelX() {
            return f -> f
            ...
               .<String, Boolean> route(s -> s.equals("[]"), m -> m
                    .channelMapping(false, "headers['channelName']")
                    .channleMapping(true, ...);
    }

there comes

Caused by: org.springframework.messaging.core.DestinationResolutionException: failed to look up MessageChannel with name 'headers['channelName']' in the BeanFactory.; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'headers['channelName']' available


Solution

  • You can just do like this:

    .route(Message.class, (m) -> m.getHeaders().get("channelName"))
    

    So, you don't need any mapping at all since you resolve to the target channel directly in the routing function.