0% found this document useful (0 votes)
150 views23 pages

MUEssentials3.7 StudentManual Mod03

This document provides instructions for consuming RESTful and SOAP web services in Mulesoft Mule. It includes walkthroughs on consuming a RESTful United Airlines flight API to return flight data. Specifically, it shows how to: - Create a new flow and add an HTTP listener connector to receive requests - Add an HTTP request connector to call the United REST API and return flight data - Modify the request to pass a destination parameter to filter the results - Set the destination dynamically from a query parameter rather than a static value The goal is to build an application that can return flight data from different airlines by consuming their web service APIs.
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)
150 views23 pages

MUEssentials3.7 StudentManual Mod03

This document provides instructions for consuming RESTful and SOAP web services in Mulesoft Mule. It includes walkthroughs on consuming a RESTful United Airlines flight API to return flight data. Specifically, it shows how to: - Create a new flow and add an HTTP listener connector to receive requests - Add an HTTP request connector to call the United REST API and return flight data - Modify the request to pass a destination parameter to filter the results - Set the destination dynamically from a query parameter rather than a static value The goal is to build an application that can return flight data from different airlines by consuming their web service APIs.
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/ 23

Module 3: Consuming Web

Services



In this module, you will learn:

About RESTful and SOAP based web services.


What RAML is and how it can be used.
To consume RESTful web services with and without RAML definitions.
To consume SOAP web services.

17

Walkthrough 3-1: Consume a RESTful web service


In this walkthrough and many others, you will work on building a Mule United Airline (MUA) application
that returns flights for Delta, United, and American airlines. In this walkthrough, you will consume a
RESTful web service that returns a list of all United flights as JSON. You will:

Create a second flow and rename flows.


Add an HTTP Listener connector endpoint to receive requests at http://localhost:8081/united.
Add an HTTP Request connector endpoint to consume a RESTful web service for United flight
data.

Make a request to the web service


1. In your computers system explorer, navigate to the student files folder for the course:
MUEssentials3.7_studentFiles_{date}.
2. Open the course snippets.txt file.
3. In a browser window, make a request to the United RESTful web service URL listed in the
course snippets.txt file; you should see JSON data for the United flights returned.
4. Look at the destination values; you should see SFO, LAX, CLE, PDX, and PDF.

18

Add a new flow with an HTTP Listener connector endpoint


5. Return to apessentials.xml.
6. Drag out another HTTP connector and drop it in the canvas above the existing
apessentialsFlow.
7. Double-click the name of the flow in the canvas and give it a new name of getUnitedFlightsFlow.

Note: You can set the name in the Properties view or directly in the blue banner.

Look at the global elements


8. Click the Global Element tabs at the bottom of the canvas.
9. Select the HTTP Listener Configuration and click Edit (or double-click it).


10. In the Global Element Properties dialog box, review the HTTP_Listener_Configuration and click
OK.

19

Configure the HTTP Listener connector endpoint


11. Click the Message Flow tab.
12. Double-click the new HTTP Listener connector endpoint.
13. Set the connector configuration to the existing HTTP_Listener_Configuration.
14. Set the path to /united.
15. Set the allowed methods to GET.

Add an HTTP Request connector endpoint


16. Drag out another HTTP connector and drop it into the process section of the flow.


17. In the Properties view, change the display name to United REST Request.
18. Click the Add button next to connector configuration.

20

19. In the Global Element Properties dialog box, set the following values and click OK.

Name: United_REST_Request_Configuration
Host: Use the value for the United web service host listed in the course snippets.txt file.
Port: Use the value for the United web service port listed in the course snippets.txt file.
Base Path: Use the value for the United web service base path listed in the course
snippets.txt file.

20. In the Properties view, set the path to / and the method to GET.

21

21. Click the Global Elements tab at the bottom of the canvas and see the new global configuration
element.
22. Return to the Message Flow view.

Test the application


23. Save the file and run the application.
24. Make a request to http://localhost:8081/united; you should see JSON flight data returned.

22

Walkthrough 3-2: Pass arguments to a RESTful web service


In this walkthrough, you will retrieve United flights for a specific destination by setting the destination as
a URI parameter. You will:

Modify the HTTP Request connector endpoint to use a URI parameter for the destination.
Set the destination to a static value.
Set the destination to a dynamic query parameter value.
Create a variable to set the destination.

Note: In a later module, you will add an HTML form to the application for destination selection.

Make a request to the web service in a browser or another tool


1. Make a request to the United RESTful web service URL for a destination listed in the course
snippets.txt file; you should see JSON data for only the flights to SFO.


2. Make additional requests for destinations of LAX, CLE, PDX, or PDF.

Add a URI parameter with a static value


3. Return to apessentials.xml.
4. Double-click the United REST Request endpoint.

23

5. In the Properties view, click the Add Parameter button.


6. Set the following parameter values.

Parameter type: uri-param


Name: destination
Value: SFO

7. Change the United REST Request endpoint path to /{destination}.

Test the application


8. Save the application to redeploy the application and make a request to
http://localhost:8081/united/; you should only get the SFO flights.

24

9. Modify the United REST Request endpoint and set the URI parameter value to LAX.
10. Save and redeploy the application and make another request to http://localhost:8081/united/;
you should now only get the LAX flights.

Add a URI parameter with a dynamic value


11. Return to the Properties view for the United REST Request endpoint.
12. Change the value of the uri-param from LAX to an expression for the value of a query
parameter called code.

#[message.inboundProperties.'http.query.params'.code]

Test the application


13. Save and redeploy the application and make a request to
http://localhost:8081/united/?code=CLE; you should only get the CLE flights.

25

Create a variable to set the destination


14. Add a Variable transformer before the United REST Request endpoint.


15. In the Variable Properties view, change the display name to Set Destination.
16. Set the operation to Set Variable and the name to destination.
17. Use a ternary expression to set the value to 'SFO' or the value of a query parameter called
code.

#[(message.inboundProperties.'http.query.params'.code == empty) ?
'SFO' : message.inboundProperties.'http.query.params'.code]



18. Navigate to the Properties view for the United REST connector endpoint.
19. Modify the URI parameter to use the new destination variable.



20. Save and redeploy the application and make a request to http://localhost:8081/united; you
should see only flights to SFO again.
21. Make another request to http://localhost:8081/united?code=PDX; you should now see flights to
PDX.


26

Walkthrough 3-3: Consume a RESTful web service that has a


RAML definition
In this walkthrough, you will consume a RESTful web service containing some simple bank account
data that has a RAML definition file. You will:

Add a third flow to the application.


Add an HTTP Listener connector endpoint to receive requests at http://localhost:8081/bank.
Add an HTTP Request connector endpoint to consume a RESTful web service defined with a
RAML file.

Add a new flow with an HTTP Listener connector endpoint


1. Return to apessentials.xml.
2. Drag out another HTTP connector and drop it in the canvas between the two existing flows.
3. Rename the flow to getBankAccountsFlow.


4. In the Properties view for the endpoint, set the connector configuration to the existing
HTTP_Listener_Configuration.

27

5. Set the path to /bank.


6. Set the allowed methods to GET.

Add an HTTP Request connector with a RAML location


7. Drag out another HTTP connector and drop it in the process section of the new flow.
8. In the Properties view, change the name to Bank REST Request.
9. Click the Add button next to connector configuration.
10. In the Global Element Properties dialog box, change the name to
Bank_REST_Request_Configuration.
11. Set the RAML location to the Banking RAML URL listed in the course snippets.txt file.
12. Wait for the RAML to be parsed and the host, port, and base path fields to be populated and
then click OK.

Note: If the fields did not populate, click the Reload RAML button next to the RAML location.


13. Click the Global Elements tab at the bottom of the canvas and see the new global configuration
element.

28

14. Return to the Message Flow view.


15. In the Bank REST Request Properties view, click the drop-down button for the path; you should
see the available resources (paths) for the RESTful web service defined by the RAML file.


16. Select the /customers path.
17. Set the method to GET.

29

Test the application


18. Save to redeploy the application and make a request to http://localhost:8081/bank; you should
see the customers returned as JSON.

Modify the request to use a path requiring a URI parameter


19. Return to the Properties view for the Bank REST Request endpoint.
20. Change the path to /customers/{customer_id}/accounts and the method to GET; a URI
parameter with the name customer_id should have been created automatically.
21. Set the customer_id parameter to a value of 2.

30

Test the application


22. Save to redeploy the application make a request to http://localhost:8081/bank; you should see
the account info for the customer with an ID of 2 returned as JSON.


23. Make a request and try to pass the customer ID as a URI parameter:
http://localhost:8081/bank/2; you should get a Resource not found response.

Get the value of an HTTP Listener endpoint URI parameter


24. Return to the Properties view for the HTTP Listener endpoint.
25. Change the path to use a wildcard to specify any path starting with /bank/.

31

26. Add a breakpoint to the Bank REST Request endpoint.


27. Save the file and debug the application.
28. Make a request to http://localhost:8081/bank/3.
29. In the Mule Debugger view, locate the http.request.uri and http.uri.params inbound properties.


30. Step through the rest of the application; you should still get the account info for the customer
with an ID of 2.
31. Return to the Properties view for the HTTP Listener endpoint.
32. Change the path to specify a URI parameter called ID: /bank/{ID}.


33. Save the file to redeploy the application in debug mode.
34. Make a request to http://localhost:8081/bank/4.

32

35. In the Mule Debugger view, locate the http.request.uri and http.uri.params inbound properties.


36. Step through the rest of the application; you should still get the account info for the customer
with an ID of 2

Set the HTTP Request endpoint URI parameter to a dynamic value


37. Return to the Properties view for the Bank REST Request endpoint.
38. Change the value of the customer_id uri-param from 2 to an expression for the value of an
HTTP Listener endpoint URI parameter called ID.
#[message.inboundProperties.'http.uri.params'.ID]

Test the application


39. Save the file and run the application.
40. Make another request to http://localhost:8081/bank/3; you should now see the account info for
the customer with an ID of 3.

33

Walkthrough 3-4: Consume a SOAP web service


In this walkthrough, you will consume a SOAP web service that returns a list of flights for Delta airlines.
You will:

Create a fourth flow with an endpoint to receive requests at http://localhost:8081/delta.


Add a Web Service Consumer connector to consume a SOAP web service for Delta flight data.
Use the DOM to XML transformer to display the SOAP response.

Browse the WSDL


1. Make a request to the Delta SOAP web service WSDL listed in the course snippets.txt file; you
should see the web service WSDL returned.
2. Browse the WSDL; you should find references to operations listAllFlights and findFlight.

34

Create a new flow with an HTTP Listener connector endpoint


3. Return to apessentials.xml.
4. Drag out another HTTP connector and drop it in the canvas above the existing flows.
5. Give the flow a new name of getDeltaFlightsFlow.
6. In the Properties view for the endpoint, set the connector configuration to the existing
HTTP_Listener_Configuration.
7. Set the path to /delta.
8. Set the allowed methods to GET.

Add a Web Service Consumer connector endpoint


9. Drag out a Web Service Consumer connector and drop it in the process section of the flow.


10. In the Properties view, set the display name to Delta SOAP Request.
11. Click the Add button next to connector configuration.
12. In the Global Element Properties dialog box, change the name to
Delta_Web_Service_Consumer.
13. Set the WSDL Location to the Delta SOAP web service WSDL listed in the course snippets.txt
file.

35

14. Wait for the WSDL to be parsed and the service, port, and address fields to be automatically
populated.

15. Click OK.


16. Switch to the Global Elements view and see the new global configuration element.
17. Return to the Message Flow view.
18. In the Delta SOAP Request Properties view, click the operation drop-down button; you should
see all of the web service operations listed.


19. Select the listAllFlights operation.

36

Debug the application


20. Add a Logger component after the Delta SOAP Request endpoint.
21. In the Logger Properties view, set the message to #[payload].
22. Add a breakpoint to the Logger.


23. Save the file and debug the application.
24. Make a request to http://localhost:8081/delta.
25. In the Mule Debugger view, drill-down into the payload variable.



26. Step to the end of the application.
27. Look at the console; you should see the type of object listed.

37

28. Return to the browser; you should see the XML SOAP response displayed there.

Display the payload as a String


29. Add a DOM to XML transformer before the Logger.

Test the application


30. Save the file to redeploy the application.
31. Make another request to http://localhost:8081/delta.

38

32. Step to the Logger; you should see the payload is now a String.


33. Resume the application.
34. Look at the console; you should see the XML SOAP response displayed there.


35. Stop the runtime.

39

You might also like