Module 5: Transforming Data: in This Module, You Will Learn
Module 5: Transforming Data: in This Module, You Will Learn
82
Walkthrough 5-1: Load external content into a message
In this walkthrough, you will add an HTML form to your application that will eventually serve as a front
end for the MUA integration application. You will:
Note: You will examine the HTML code soon – after you load the form and see what it looks like.
Create a flow
3. Return to apessentials.xml.
4. Drag out another HTTP connector to create a new flow at the top of the canvas and name it
getFlightFormFlow.
5. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
6. Set the path to /flights and set the allowed methods to GET.
83
Set the payload to HTML and JavaScript
7. Add a Parse Template transformer to the process section of the flow.
8. In the Properties view for the transformer, set the location to FlightFinder.html.
84
14. Step through the rest of the flow and then return to the browser window; you should see the
HTML form.
85
Walkthrough 5-2: Transform JSON to an object
In this walkthrough, you will work with the data posted from the HTML form. You will:
86
10. Look at the Mule Debugger view; you should see the return data is a BufferInputStream.
16. Resume code execution and look at the console; you should get a NonSerializableException.
87
Add Java classes file to the project
17. On your computer, navigate to the java folder in the student files folder for the course:
APEssentials3.6_studentFiles_{date}/java.
18. Copy and paste (or drag) the com.mulesoft.training folder into your Anypoint Studio project’s
src/main/java folder.
88
Debug the application
22. Save the file to redeploy the application.
23. Make a request to http://localhost:8081/flights and submit the form again.
24. Step to the Logger in the getFlightsFlow; you should now see the see the form data as a
FlightRequest object.
27. Select Set Variable and set the name to destination and the value to #[payload.destination].
89
Test the application
28. Save the file to redeploy the application.
29. Make a request to http://localhost:8081/flights and submit the form again.
30. Step to the Logger and click the Variables tab; you should see your flow variable.
Note: You will continue working with this flow in a later walkthrough. You will add functionality to
get the requested flight data from the other flows and return it back to the form.
90
Walkthrough 5-3: Transform JSON to a collection of objects
In this walkthrough, you will work with the United flight data. You will:
• Transform the United flight data from JSON to a collection of Flight objects.
• Use the JSON to Object transformer with a return class using Jackson annotations.
91
7. Save the file to redeploy the application.
8. Make a request to http://localhost:8081/united; you should get a NonSerializableException in the
console.
92
15. Step through the rest of the flow and then return to the request window; you should get some
garbled data or a download file – your request tool’s interpretation of the complex object.
21. Locate the error details in the console; you should see a reference to unrecognized origin.
93
23. Add a @JsonProperty annotation to map the following JSON keys to Flight properties.
• code to flightCode
• origin to origination
• emptySeats to availableSeats
24. Click one of the Error icons next to the line numbers and double-click Import JsonProperty
(code. org.codehaus.jackson.annotate.JsonProperty).
25. Save the file.
94
Return only the collection of flights
29. Add a Set Payload transformer after the JSON to Object transformer.
30. Set it’s display name to Collection of Flights and its value to #[payload.flights].
Note: If you do not see your message, change your Logger level to WARN.
95
Walkthrough 5-4: Transform XML to a collection of objects
In this walkthrough, you will work with the Delta flight data. You will:
• Transform the Delta flight data from XML to a collection of Flight objects.
• Use the XML to JAXB Object transformer with a return class using JAXB annotations.
96
8. Click the Add button next to context.
9. In the Global Element Properties dialog box, set the package name to com.mulesoft.training and
click OK.
10. Back in the Properties view, set the return class to com.mulesoft.training.FlightArray.
11. Save the file to redeploy the application; you should get an error in the console that
com.mulesoft.training does not have a jaxb.ini file.
97
13. In the New file dialog box, set the file name to jaxb.index and click Finish.
14. Open jaxb.index and list the two Java classes to be used in the mapping.
20. Step through the rest of the flow and then return to the request window.
98
Add JAXB annotations to the FlightArray class
21. Return to FlightArray.java.
22. Add an @XmlElement annotation to map the XML return node to the flights property; be sure to
import the appropriate class (javax.xml.bind.annotation.XmlElement).
25. Add an @XmlAccessorType annotation for the class set to the static value,
XmlAccessType.Field; be sure to import the appropriate classes.
29. Explore the data and notice that availableSeats is always zero and the flightCode and
origination null.
99
Add JAXB annotations to the Flight class
30. Return to Flight.java.
31. Add an @XmlAccessorType annotation for the class set to the static value,
XmlAccessType.Field; be sure to import the appropriate classes.
32. Add an @XmlElement annotation to map the XML flightCode node to the code property; be sure
to import the appropriate class.
33. Add an @XmlElement annotation to map the XML origin node to the origination property.
34. Add an @XmlElement annotation to map the XML emptySeats node to the availableSeats
property.
100
37. Step through the rest of the flow and then return to the request window; the FlightArray of
Flights is automatically serialized for return and display.
101
Walkthrough 5-5: Use the DataMapper to map complex objects
In this walkthrough, you will work with the American flight data. You will:
• Transform the American flight data from a List of Map objects to a collection of Flight objects.
• Use the DataMapper transformer.
Add a DataMapper
1. Locate the getAmericanFlightsFlow in apessentials.xml.
2. Delete the Object to String transformer.
3. Replace it with a DataMapper.
4. Look at the DataMapper Properties view; the input should be automatically populated from
DataSense as a List<Map> because the Database connector configuration is DataSense
enabled by default.
102
6. Beneath the type, click the button next to Class.
7. In the Object Introspector window that opens, click the Click here to select an option link and
select Collection.
8. Click the Click here to select an option link again and select Java Object.
9. In the Select entries window that opens, enter Fli and then press the Enter key to add an entry
of com.mulesoft.training.Flight.
103
Create the mapping
12. Click the Create mapping button at the bottom of the Properties view; some of the fields in the
objects had the same names so were automatically mapped.
13. Drag and drop fields from the left pane to the right pane to make the following mappings.
14. Select flightCode in the right pane and see the concatenation expression below the right pane.
104
Debug the application
15. Save the file.
16. Add a breakpoint to the Database component and debug the application.
17. Make a request to http://localhost:8081/american.
18. Watch the payload value and step through the flow; you should get a NumberFormatException
for the input string “none”.
105
24. Watch the payload value and step through the flow; you should now see the payload
transformed into an ArrayList of Flight objects.
106
Walkthrough 5-6: Use the DataMapper to pass arguments to a
SOAP web service
In this walkthrough, you will work with the Delta flight data. You will:
• Return the flights for a specific destination instead of all the flights.
• Change the web service operation invoked to one that requires a destination as an input
argument.
• Use the DataMapper to pass an argument to a web service operation.
• Create a variable to set the destination to a dynamic query parameter value.
107
Add a DataMapper
5. Add a DataMapper transformer to the left of the Delta SOAP Request endpoint.
6. In the DataMapper Properties view, verify that the output target and type are set to Xml
<findFlight>; the Web Service Consumer global element is set to use DataSense by default.
Note: It does not matter which value you select; it will not be used, as you will see in the next
steps.
9. In the graphical mapping editor, right-click Input arguments and select Add Input argument.
108
10. In the New input argument dialog box, set the following values and click OK.
• Name: destination
• Type: string
• Mule expression: SFO
11. Drag and drop the destination input argument to the destination argument of findFlight.
109
Set a flow variable to hold the value of a query parameter
15. Select the Set Destination transformer in the getUnitedFlightsFlow and from the main menu,
select Edit > Copy (or press Cmd+C).
16. Click in the process section of the getDeltaFlightsFlow and from the main menu, select Edit >
paste (or press Cmd+V); you should see a copy of the transformer added to the flow.
17. Move the transformer before the DataMapper.
18. In the Variable Properties view, change the display name to Set Destination and review the
expression.
Modify the input argument to use a dynamic destination from a query parameter
19. In the Properties view for the DataMapper, double-click the Input Argument to modify it.
20. Change the value from SFO to use the flow variable, #[flowVars.destination].
110
24. Look at the price of each flight; the flights are not ordered by price.
111
Walkthrough 5-7: Use a custom Java transformer
In this walkthrough, you will use a custom transformer to sort the airline flight results from lowest to
highest price. You will:
112
4. Review the code; because the class only needs to operate on the payload (to sort it) and not the
rest of the message, it implements the org.mule.api.transformer.Transformer class.
113
Debug the application
9. Save the file to redeploy the application in debug mode.
10. Make a request to http://localhost:8081/united?code=CLE.
11. Step through to the Logger; you should see the collection has been reordered by price.
Note: If the sort is not working, the visual view and the XML may be out of sync. Check the
order of the elements in the XML and fix them if necessary.
114