GenericHandler is a Spring Integration functional interface for processing a message payload and returning a result. Its handler receives both the payload and MessageHeaders, making it useful when a service activator transforms or persists data before continuing an integration flow. section 10.2.6
Use GenericHandler when:
- A service activator should process an incoming payload and return a new payload for an output channel. section 10.2.6
- A Java DSL integration flow needs a payload-and-headers lambda or method reference passed to handle(). section 10.2.6
- The handler ends the flow; return null so Spring Integration does not look for an output channel. section 10.2.6 section 10.3
Submitting email orders
An OrderSubmitMessageHandler implements GenericHandler<EmailOrder>. Its handle() method receives an EmailOrder and MessageHeaders, submits the order with an injected RestTemplate, and returns null because the handler is the final step in the flow. In another flow, a GenericHandler saves an EmailOrder through an OrderRepository and returns the saved object to completeChannel. section 10.3 section 10.2.6
@Bean
@ServiceActivator(inputChannel="orderChannel",
outputChannel="completeChannel")
public GenericHandler<EmailOrder> orderHandler(
OrderRepository orderRepo) {
return (payload, headers) -> {
return orderRepo.save(payload);
};
}
Browse a complete service-activator declaration that accepts an EmailOrder, saves it through a repository, and returns the result. section 10.2.6
