2
votes

what is the purpose of #[message.payloadAs(java.lang.String)] in mule esb??

Can some one explain me with an example. When getting a response from a webservice to my mule esb. When i placed simple logger with #[payload] I am able to see some simple hashcode in the logs like @1b8353(but not the payload message xml). But when logged using #[message.payloadAs(java.lang.String)] I am able to see payload xml being logged clearly. Can someone explain its purpose clearly ??

Please comment for further clarification!!!!

1

1 Answers

4
votes

#[message.payloadAs(java.lang.String)] Will try and transform the payload to the given desired class. In this case a String.

Whereas logging just payload will log whatever the current payload representation is.

The method payloadAs(Class<T> outpuTType) is on the Message context class which in turn calls the MuleMessage.getPayloadAs(Class<T> outputType) I believe:

http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MuleMessage.html#getPayload(java.lang.Class)

Will attempt to obtain the payload of this message with the desired Class type. This will try and resolve a transformer that can do this transformation. If a transformer cannot be found an exception is thrown. Any transformers added to the registry will be checked for compatibility Parameters: outputType - the desired return type Returns: The converted payload of this message. Note that this method will not alter the payload of this message unless the payload is an InputStream in which case the stream will be read and the payload will become the fully read stream. Throws: TransformerException - if a transformer cannot be found or there is an error during transformation of the payload