Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Transforming Data in a Flow

This example shows how to use a custom transformer. In this case, a simple transformer is used in a flow to alter information received in an HTTP request before passing the information to a custom component.

What you Will Learn

  • How to create a Custom Transformer in Mule Studio.

  • How to configure the Transformer in the flow.

  • How to implement a Custom Transformer.

Watch the video

See this example built and run in Mule Studio

10 minutes

Build it Now!

Prerequisites

This examples requires that you run through the example described in Adding Business Logic to a Flow.

Building the Example

\1. Let’s begin with the "Hello" flow you built in Adding Business Logic to a Flow. Run it and confirm in a browser window that when you go to http://localhost:8082/Fred you see the following on screen:

image

Now, wouldn’t it be nice to remove that slash before the name? A Custom Transformer can take care of that.

\2. Create a new class in the org.mulesoft.example.hello package

image

\3. Name it NameTransformer, and make it extend AbstractTransformer

image

\4. Implement it as follows. It will detect Strings beginning with a slash and remove it. Otherwise, it will return the original object.

image

\5. Open your flow. It should look like this:

image

\6. Insert a Java Transformer. Do so by dragging it from the sidebar and dropping it between the HTTP Inbound Endpoint and the Hello Component.

image In previous Studio versions, the Java Transformer was named Custom Transformer.

image

\7. Double-click on the Transformer you just inserted and insert the full name of the Transformer class we just implemented. You may also want to add some additional documentation information. Click OK when you are done and save the flow.

image

image

Running the Example

\8. Run as Mule Application (as you did in the previous examples).

\9. Open a browser window and go to http://localhost:8082/Fred. Notice that this time the greeting does not contain a slash:

image

What Just Happened?

  • You created a Custom transformer and plugged it into the flow.

  • Your HTTP request payload was processed by the Transformer before passing it to the Component.

  • The HTTP Endpoint, the Transformer and the Component don’t know about each other. Mule connects them as necessary.