0% found this document useful (0 votes)
83 views33 pages

Module 5: Transforming Data: in This Module, You Will Learn

- This module covers transforming data between different formats like XML, JSON, and Java objects. - You will learn about different transformer types and how to use JAXB and Jackson annotations to map complex XML and JSON to Java objects. - The walkthroughs demonstrate loading an HTML form, transforming the form data to a Java object, and transforming JSON flight data to a collection of custom Flight objects using Jackson annotations.

Uploaded by

RafiqhiAssegaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views33 pages

Module 5: Transforming Data: in This Module, You Will Learn

- This module covers transforming data between different formats like XML, JSON, and Java objects. - You will learn about different transformer types and how to use JAXB and Jackson annotations to map complex XML and JSON to Java objects. - The walkthroughs demonstrate loading an HTML form, transforming the form data to a Java object, and transforming JSON flight data to a collection of custom Flight objects using Jackson annotations.

Uploaded by

RafiqhiAssegaf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Module 5: Transforming Data

In this module, you will learn:

• About the different types of transformers.


• To transform objects to and/from XML and JSON.
• To do more complicated JSON to object mappings with Jackson annotations.
• To do more complicated XML to object mappings with JAXB annotations.
• To streamline complex data transformations with DataSense and the DataMapper.
• To create a custom transformer with Java.

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:

• Create a new flow that receives GET requests at http://localhost:8081/flights.


• Use the Parse Template transformer to load the content of an HTML file into a flow.

Add an HTML file to the project


1. On your computer, navigate to the student files folder for the course,
APEssentials3.6_studentFiles_{date}.
2. Copy and paste (or drag) the resources/FlightFinder.html file into your project’s
src/main/resources folder.

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.

Debug the application


9. Add a breakpoint to the Parse Template transformer.
10. Add a Logger component after the transformer.
11. Save the file and debug the application.
12. In a browser, make a request to http://localhost:8081/flights.
13. Step through the flow and watch the value of the payload change.

84
14. Step through the rest of the flow and then return to the browser window; you should see the
HTML form.

15. Click the Find Flights button; what happens?

Review the HTML form code


16. Open FlightFinder.html in Anypoint Studio.
17. Locate the form at the bottom and find the names of the select boxes and their option values.

18. Locate to where the form data is posted.

19. Locate in what format the data is that is posted.

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:

• Create a new flow that receives POST requests at http://localhost:8081/flights.


• Transform the form data from JSON to a FlightRequest object and save the destination in a flow
variable.
• Use the JSON to Object transformer.

Create a new flow


1. Return to apessentials.xml.
2. Drag out another HTTP connector to create a new flow and name it getFlightsFlow
3. In the HTTP Properties view, set the connector configuration to the existing
HTTP_Listener_Configuration.
4. Set the path to /flights (the same as the form flow) and set the allowed methods to POST.

Look at the data posted from the form


5. Add a Logger to the flow and add a breakpoint to it.
6. Save the file to redeploy the application in debug mode.
7. Make another request to http://localhost:8081/flights.
8. Step through or remove the breakpoints in the getFlightsFormFlow to get to the form in the
browser window.
9. Click the Find Flights button.

86
10. Look at the Mule Debugger view; you should see the return data is a BufferInputStream.

Add a JSON to Object transformer


11. Add a JSON to Object transformer before the Logger.
12. Add a breakpoint to the JSON to Object transformer if it does not have one.

Debug the application


13. Save the file to redeploy the application in debug mode.
14. Make a request to http://localhost:8081/flights and submit the form again.
15. Step to the Logger in the getFlightsFlow; you should see the form data, but it is still JSON.

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.

19. Open the FlightRequest.java class and examine the code.

Set the transformer return type


20. Return to apessentials.xml.
21. In the JSON to Object Properties view, set the return class to
com.mulesoft.training.FlightRequest.

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.

Store form data in a flow variable


25. Add a Variable transformer after the JSON to Object transformer.
26. In the Properties view, set the display name to Set Destination.

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.

31. Stop the Mule Debugger.

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.

Review the United flight data


1. Return to apessentials.xml.
2. Run the application and make a request to http://localhost:8081/united.
3. Look at the names of the object keys.

Transform JSON to a general collection of objects


4. Locate the getUnitedFlightsFlow.
5. Add a JSON to Object transformer after the United REST Request.
6. Add a Logger after the transformer.

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.

9. Navigate to the Properties view for the JSON to Object transformer.


10. Set the return class to java.util.Map.

Debug the application


11. Add a breakpoint to the JSON to Object transformer.
12. Save the file and debug the application.
13. Make a request to http://localhost:8081/united.
14. Watch the payload value and step through the flow; you should see the payload transformed to
a collection of objects.

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.

Transform JSON to a collection of custom objects


16. Open the FlightArray and Flight classes and examine the code.
17. Return to the Properties view for the JSON to Object transformer in apessentials.xml.
18. Change the return class to com.mulesoft.training.FlightArray.

Test the application


19. Save the file to redeploy the application.
20. Make a request to http://localhost:8081/united and step through the application; you should get
a Failed to transform error.

21. Locate the error details in the console; you should see a reference to unrecognized origin.

Add Jackson annotations to the Flight class


22. Return to Flight.java.

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.

Debug the application


26. Return to apessentials.xml.
27. Debug the application and make another request to http://localhost:8081/united.
28. Watch the payload value and step through the flow; you should see the payload transformed to
a FlightArray of Flight objects.

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].

31. Set the Logger message to #[payload].

Test the application


32. Save the file to redeploy the application.
33. Make another request to http://localhost:8081/united; you should now see just the collection of
objects returned.

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.

Review the Delta flight data


1. Return to apessentials.xml and locate the getDeltaFlightsFlow.
2. Make sure the application is running and make a request to http://localhost:8081/delta.
3. Look at the names of the XML elements.

Add and configure an XML to JAXB Object transformer


4. Delete the XML to DOM transformer.
5. Add an XML to JAXB Object transformer after the Delta SOAP Request.
6. Navigate to the XML to JAXB Object Properties view; you should see a message that a
jaxbContext-ref is required.
7. Scroll down in the Properties view and locate the Settings > Context section.

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.

Create a jaxb.index file


12. Right-click the com.mulesoft.training package in the Package Explorer and select New > 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.

15. Save the file.

Debug the application


16. Return to apessentials.xml.
17. Make sure there is a breakpoint on the Delta SOAP Request and debug the application.
18. Make a request to http://localhost:8081/delta.
19. Watch the payload value and step through the flow; you should see the payload transformed to
a FlightArray but the flights property is null – there wasn’t enough data to map the XML to the
objects correctly.

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).

23. Save the file.


24. If you want, run the application again and make a request to http://localhost:8081/delta; you will
get an IllegalAnnotationsException.

25. Add an @XmlAccessorType annotation for the class set to the static value,
XmlAccessType.Field; be sure to import the appropriate classes.

26. Save the file.

Debug the application


27. Debug the application and make a request to http://localhost:8081/delta.
28. Watch the payload value and step through the flow; you should see the payload transformed to
a FlightArray of Flight objects.

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.

Debug the application


35. Debug the application again and make a request to http://localhost:8081/delta.
36. Watch the payload value and step through the flow; you should see the payload successfully
transformed to a FlightArray of Flight objects with all fields properly mapped.

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.

Return only the collection of flights


38. Return to apessentials.xml.
39. Add a Set Payload transformer after the XML to JAXB Object transformer.
40. Set it’s display name to Collection of Flights and its value to #[payload.flights].

Test the application


41. Save the file to redeploy the application.
42. Make a request to http://localhost:8081/united.
43. Step though or resume the application; you should now see just the collection of objects
returned.

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.

Configure the DataMapper output


5. In the Output section, set the type to Pojo.

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.

10. Click OK.


11. Look at the output section; the class should now be set to a collection of Flight objects.

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.

• toAirport > destination


• seatsAvailable > availableSeats
• fromAirport > origination
• takeOffDate > departureDate
• code1 > flightCode
• code2 > flightCode

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”.

Modify the DataMapper


19. Return to the DataMapper Properties view.
20. Click the Script button in the upper-right corner of the Properties view.
21. In the Script window, modify the expression setting availableSeats to use a ternary expression
to set it to 0 or to the input value.

output.availableSeats  =  input.seatsAvailable  ==  "none"  ?  0  :    


  str2integer(input.seatsAvailable);  
Note: Be sure to end the line with a semi-colon.

Debug the application


22. Save the file to redeploy the application.
23. Make a request to http://localhost:8081/american.

105
24. Watch the payload value and step through the flow; you should now see the payload
transformed into an ArrayList of Flight objects.

25. Stop the Mule Debugger.

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.

Call a different web service operation


1. Return to apessentials.com and locate the getDeltaFlightsFlow.
2. In the Properties view for the Delta SOAP Request endpoint, change the operation to findFlight.

3. Apply the changes and run the application.


4. Make a request to http://localhost:8081/delta; you should get an error because you are calling
an operation that requires parameters but you have not passed it any.

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.

Use the DataMapper to pass an argument to the web service


7. Set the Input Source to any one of the inbound properties that is of type String, like http.method.

Note: It does not matter which value you select; it will not be used, as you will see in the next
steps.

8. Click the Create mapping button.

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.

Test the application


12. Save the file and debug the application.
13. Make a request to http://localhost:8081/delta; you should now see only the SFO flights.

14. Step through the rest of the application.

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].

Test the application


21. Save the file and redeploy the application.
22. Make a request to http://localhost:8081/delta; you should now only see flights to SFO.
23. Make another request to http://localhost:8081/delta?code=CLE; you should now see flights to
CLE.

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:

• Review a pre-written Java class to be used as the transformer class.


• Use a custom Java transformer.
• Sort the United flight data by price.

Review the Java class


1. Open Flight.java.
2. Review the code; notice that it implements Comparable and has a compareTo() function.

3. Open the FlightSortTransformer class, which is also located in the com.mulesoft.training


package of your project.

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.

Add a Java transformer


5. Navigate to the getDeltaFlightsFlow in apessentials.xml.
6. Add a Java transformer after the Collection of Flights transformer.
7. In the Java Properties view, set the display name to Sort by Price.

8. Set the transformer class to com.mulesoft.training.FlightSortTransformer.

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.

12. Stop the Mule Debugger.

114

You might also like