In a Spring Integration flow, a handler processes a message after it reaches a channel. A service activator can send that message to a MessageHandler. When processing should produce a payload for another channel, use a GenericHandler instead. section 10.2.6
Choose the contract by what the flow needs next:
- MessageHandler receives the whole Message. A small lambda can read its payload and print it. section 10.2.6
- GenericHandler receives a typed payload and message headers. It can return a saved order for an output channel; the example does not use the headers. section 10.2.6
- Both can be supplied to IntegrationFlows.handle() instead of declared as a service activator bean. A MessageHandler lambda receives a message, not just its payload. section 10.2.6
@Bean
@ServiceActivator(inputChannel="orderChannel",
outputChannel="completeChannel")
public GenericHandler<EmailOrder> orderHandler(
OrderRepository orderRepo) {
return (payload, headers) -> {
return orderRepo.save(payload);
};
}
Here, @ServiceActivator connects orderChannel to a GenericHandler<EmailOrder>. The handler saves the payload and returns the saved order for completeChannel. section 10.2.6
Finish an email-to-order flow
An email transformer produces a message carrying an EmailOrder. A GenericHandler implementation receives the order, uses an injected RestTemplate to POST it to a configured API URL, and returns null to mark the end of the flow. This contrasts with returning a saved order when another channel should receive the result. section 10.3 section 10.2.6
Listing 10.7 Posting orders to the Taco Cloud API via a message handler
package tacos.email;
import org.springframework.integration.handler.GenericHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class OrderSubmitMessageHandler
implements GenericHandler<EmailOrder> {
private RestTemplate rest;
private ApiProperties apiProps;
public OrderSubmitMessageHandler(ApiProperties apiProps, RestTemplate rest) {
this.apiProps = apiProps;
this.rest = rest;
}
@Override
public Object handle(EmailOrder order, MessageHeaders headers) {
rest.postForObject(apiProps.getUrl(), order, String.class);
return null;
}
}
The complete handler shows the handle(EmailOrder, MessageHeaders) method, constructor-injected dependencies, REST call, and null return. section 10.3





















