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
