liveBook · Manning

GenericHandler

As explained in Spring in Action, Sixth Edition

GenericHandler is a Spring Integration functional interface for handling a message payload and returning a result.

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

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines payload, headers, returned results, and terminal null behavior, but does not detail interface internals.
Examples
Uses order persistence and email-order submission to a Taco Cloud API as concrete handler examples.
What it assumes
Assumes basic Spring Integration messages and flows while introducing payloads, headers, service activators, and terminal handlers.
What you can do after
Apply it via Spring Integration to process payloads, access headers, return results, or terminate flows.
How it connects
Connects service activators, MessageHandler, message headers, integration flows, transformers, repositories, and RestTemplate.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.