The <transformer> element is used to create a Message-transforming endpoint. In addition to "input-channel" and "output-channel" attributes, it requires a "ref". The "ref" may either point to an Object that contains the @Transformer annotation on a single method (see below) or it may be combined with an explicit method name value provided via the "method" attribute.
<transformer id="testTransformer" ref="testTransformerBean" input-channel="inChannel" method="transform" output-channel="outChannel"/> <beans:bean id="testTransformerBean" class="org.foo.TestTransformer" />
Using a "ref" attribute is generally recommended if the custom transformer handler implementation can be reused in
other <transformer>
definitions. However if the custom transformer handler implementation should
be scoped to a single definition of the <transformer>
, you can define an inner bean definition:
<transformer id="testTransformer" input-channel="inChannel" method="transform" output-channel="outChannel"> <beans:bean class="org.foo.TestTransformer"/> </transformer>
Note | |
---|---|
Using both the "ref" attribute and an inner handler definition in the same |
The method that is used for transformation may expect either the Message
type or
the payload type of inbound Messages. It may also accept Message header values either individually or as a full
map by using the @Header and @Headers parameter annotations respectively. The return value of the method can be
any type. If the return value is itself a Message
, that will be passed along to
the transformer's output channel. If the return type is a Map, and the original Message payload was
not a Map, the entries in that Map will be added to the Message headers of the original
Message (the keys must be Strings). If the return value is null, then no reply Message will
be sent (effectively the same behavior as a Message Filter returning false). Otherwise, the return value will be
sent as the payload of an outbound reply Message.
There are a also a few Transformer implementations available out of the box. Because, it is fairly common
to use the toString()
representation of an Object, Spring Integration provides an
ObjectToStringTransformer
whose output is a Message with a String payload. That String
is the result of invoking the toString operation on the inbound Message's payload.
<object-to-string-transformer input-channel="in" output-channel="out"/>
A potential example for this would be sending some arbitrary object to the 'outbound-channel-adapter' in the
file namespace. Whereas that Channel Adapter only supports String, byte-array, or
java.io.File
payloads by default, adding this transformer immediately before the
adapter will handle the necessary conversion. Of course, that works fine as long as the result of the
toString()
call is what you want to be written to the File. Otherwise, you can
just provide a custom POJO-based Transformer via the generic 'transformer' element shown previously.
Tip | |
---|---|
When debugging, this transformer is not typically necessary since the 'logging-channel-adapter' is capable of logging the Message payload. Refer to the section called “Wire Tap” for more detail. |
If you need to serialize an Object to a byte array or deserialize a byte array back into an Object, Spring Integration provides symmetrical serialization transformers.
<payload-serializing-transformer input-channel="objectsIn" output-channel="bytesOut"/> <payload-deserializing-transformer input-channel="bytesIn" output-channel="objectsOut"/>
If you only need to add headers to a Message, and they are not dynamically determined by Message content, then referencing a custom implementation may be overkill. For that reason, Spring Integration provides the 'header-enricher' element.
<header-enricher input-channel="in" output-channel="out"> <header name="foo" value="123"/> <header name="bar" ref="someBean"/> </header-enricher>