liveBook · Manning

Transformer interface

As explained in Spring in Action, Sixth Edition

GenericTransformer is a Spring Integration functional interface for transforming a message payload or message into another value.

A transformer changes a message as it moves through a Spring Integration flow. It might uppercase a String, convert an Integer to another type, or turn an email into an order. A channel carries the input to the transformer, and another channel carries the result onward. section 10.2 section 10.2.3

For a straightforward conversion, use GenericTransformer as a functional interface. Its input and output type parameters make the conversion clear, and its implementation can be a lambda or method reference. In Java configuration, put @Transformer on the bean and name its inputChannel and outputChannel. Spring Integration then takes values from the input channel, applies the conversion, and publishes the result to the output channel. section 10.1.2 section 10.2.3

Uppercase text before writing a file

Declare a GenericTransformer<String, String> whose lambda returns text.toUpperCase(). Annotate the bean with @Transformer(inputChannel="textInChannel", outputChannel="fileWriterChannel"). Text submitted through the gateway reaches the transformer; a file-writing handler consumes the uppercase result. The handler is a separate component: the transformer changes the value, while the handler writes it. section 10.1.2 section 10.2

@Bean
@Transformer(inputChannel="numberChannel",
             outputChannel="romanNumberChannel")
public GenericTransformer<Integer, String> romanNumTransformer() {
  return RomanNumbers::toRoman;
}

This bean shows a typed Integer-to-String GenericTransformer. @Transformer connects numberChannel to romanNumberChannel, while the method reference supplies the conversion. section 10.2.3

Choose the form that fits the flow:

  • Use an annotated GenericTransformer bean when you want the conversion and its channel names declared together. section 10.1.2
  • Use the Java DSL’s transform() step to place a lambda, method reference, or injected transformer directly in the flow definition. section 10.1.3 section 10.2.3
  • For email-specific processing, extend AbstractMailMessageTransformer instead; it supplies extracted mail information to doTransform() before the order is built. section 10.3

XML configuration offers another option: a transformer can use a SpEL expression to uppercase the payload between named channels. It does not require a GenericTransformer bean. section 10.1.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows typed beans, annotations, lambdas, method references, and DSL alternatives; stops short of interface internals.
Examples
Demonstrates uppercase file output, Integer-to-Roman-numeral conversion, and email-to-order transformation.
What it assumes
Introduces transformer and channel roles while assuming enough Java to read lambdas, method references, and bean declarations.
What you can do after
Build it from scratch as an annotated GenericTransformer bean or add a conversion through the Spring Integration Java DSL.
How it connects
Links transformers to message channels, gateways, service activators, channel adapters, and integration flows.

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.