Building RESTful Web Services with Mulesoft Anypoint Platform for APIs
In this walkthrough, you will create an API definition for MUA with RAML. You will:
• Add a new API to the Anypoint Platform.
• Use the API Designer to create a RAML file.
• Use a nested resource for the destination and a query parameter for the airline.
Add a new API
1. In a browser, go to http://anypoint.mulesoft.com and log in.
2. Click the APIs link in the main menu bar.
3. Click the Add new API button.
4. In the Add API dialog box, enter the following and then click Add.
• API name: MUA Flights API
• API name: MUA Flights API
• Version name: 1.0
• Description: Returns flights for a given airport code destination
Note: To change the background color from black to white, press Ctrl+Shift+T.
Add RAML root metatdata
7. Place the cursor on a new line of code at the end of the file.
8. Click the baseUri element in the API Designer shelf to add it to the RAML file.
Note: Leave the default value #baseUri: http://server/api/{version} for now during development.
Add a RAML resource
9. Go to a new line of code and add a resource called flights.
/flights:
Add a nested RAML resource
10. Indent by pressing the Enter key and then the Tab key.
11. Add a nested resource to return flights for a particular destination.
/flights:
/{destionation}:
Add a RAML method
/{destionation}:
Add a RAML method
12. Go to a new line of code, press the Tab key, and then press the G key and then the Enter key to add a get method.
13. Indent by pressing the Enter key and then the Tab key.
14. Scroll down in the API Designer shelf and locate and click responses
15. Indent and type 200:.
responses:
200:
16. Indent and type b and then press Enter to add a body parameter (or add it from the shelf).
17. Indent and type a and then press Enter to add application/json (or add it from the shelf).
Note: This is the minimal RAML file needed for Anypoint Studio and APIkit to generate the RESTful interface.
Add a query parameter
18. Go to a new line of code after get:.
19. Press the q key and then the Enter key to add queryParameters.
20. Indent and add a query parameter called airline.
21. Indent and add a default property and set it to all.
22. Return and add an enum property and set it to an array with values all, united, delta, and American
Use API Designer to simulate an API
In this walkthrough, you will add example responses to the API definition and test it by running a live simulation. You will:
In this walkthrough, you will add example responses to the API definition and test it by running a live simulation. You will:
• Use the API console in API Designer.
• Use RAML to specify example responses for your API.
• Use the API Designer mocking service to run a live simulation of your API.
Use the API Designer console
1. Return to your MUA Flights API in API Designer.
2. Look at the API console.
Note: If you do not see the console, click the arrow located in the middle of the right edge of the browser window.
3. Click the GET button for the /flights/{destination} resource;; you should see the request information.
4. Scroll down to the Responses section and see the specification for a 200 status code.
5. Scroll back up and click the Try it button.
6. Enter a destination of SFO and click the GET button.
Use the mocking service
8. Locate the Mocking Service slider in the menu bar above the console.
9. Slide it to on.
13. Close the /flights/{destination} window.
Add examples
14. Return to the course snippets.txt file and copy the Example flights JSON.
[{"airlineName":"United","price":400,"departureDate":"2015/03/20", "planeType":"Boeing 737","origination":"MUA","flightCode":"ER38sd", "availableSeats":0,"destination":"SFO"},{"airlineName":"United", "price":945,"departureDate":"2015/09/11","planeType":"Boeing 757", "origination":"MUA","flightCode":"ER39rk","availableSeats":54, "destination":"SFO"},{"airlineName":"United","price":954, "departureDate":"2015/02/12","planeType":"Boeing 777", "origination":"MUA","flightCode":"ER39rj","availableSeats":23, "destination":"SFO"}]
15. Return to the API Designer editor and indent from the application/json element and add an example element.
16. Add a space and then a | after the example element.
17. Indent and paste the JSON array you just copied.
View the example data in the simulation
18. Return to the API console.
19. Click the GET button for the /flights/{destination} resource again.
20. Click Try it, enter a destination, and click GET.
21. Look at the response body;; you should now get your example data.
Save and export the RAML file
22. Slide the Mocking Service slider to off.
23. Save the file.
24. Right-click mua.raml in the left pane and select Rename.
25. Change the name to mua.raml and click OK.
26. Click the Export files button to download the RAML file.
27. Locate the MUA_Flights_API-v1.0.zip file that is downloaded.
28. Expand the ZIP and locate the mua.raml file it contains.
Use Anypoint Studio to create a RESTful API interface from a RAML file
In this walkthrough, you will generate a RESTful interface from the RAML file. You will:
• Add a RAML file to your apessentials project.
• Use Anypoint Studio and APIkit to generate a RESTful web service interface from a RAML file.
• Test the web service in the APIkit Consoles view and a browser.
Add the RAML file
1. Drag the mua.raml file into the src/main/api folder in your apessentials project.
Create a RESTful interface
2. Right-click the RAML file and select APIkit > Generate Flows.
Note: If Generate Flows is disabled, there is an error in your RAML file and you need to return to API Designer and fix it and then re-add it to Anypoint Studio.
3. Wait until a new mua.xml file is generated and opened.
Modify the HTTP endpoint
4. In mua.xml, double-click the HTTP connector in the mua-main flow.
5. Look at the path.
6. Click the Edit button next to Connector Configuration.
7. In the Global Element Properties dialog box, view the host and port values and click OK.
8. Change the connector configuration to the existing HTTP_Listener_Configuration.
9. Switch to the Global Elements view.
10. Select the mua-httpListenerConfig element and click Delete.
11. Return to the Message Flow view.
Examine the APIkit router
12. Double-click the APIkit Router.
13. In the APIkit Router Properties view, see the router configuration is set to mua-config. 14. Click the Edit button next to Connector Configuration.
Look at the generated resource flow
16. Look at the name of the other flow created: get:/flights/{destination}:mua-config.
18. Switch to the Configuration XML view and look at the generated code.
Test the web service in the APIkit Consoles view
19. Save the file and run the application.
20. Look at the APIkit Consoles view that opens.
Note: If the API is not displayed in the APIkit Console, change your HTTP Listener Configuration host from 0.0.0.0 to localhost.
21. Click the GET button for /flights/{destination}.
22. Click the Try It button.
23. Enter a destination of LAX (or any other value).
24. Click in the airline query parameter field and select one of the enumerated values, like delta.
25. Click the GET button;; you should see the example data displayed.
Test the web service in a browser
26. Go to a browser, and make a request to http://localhost:8081/api/flights/LAX (or any other destination);; you should see the example data returned.
Use Anypoint Studio to implement a RESTful web service
In this walkthrough, you will wire the RESTful web service interface up to your back end logic. You will:
In this walkthrough, you will wire the RESTful web service interface up to your back end logic. You will:
• Split the getFlightsFlow into two flows: one that transforms the form post data into a FlightRequest object and the other that gets all the flights.
• Determine how to reference the web service destination and airline values.
• Call the backend flow.
• Test the web service in the APIkit Consoles view and a browser.
Modify the existing flows
1. Return to getFlights.xml.
2. Drag out a Flow scope element from the palette and drop it at the top of the canvas.
3. Give it a display name of getFlightsFromFormFlow.
4. Drag the HTTP endpoint from getFlightsFlow and drop it in the source section of getFlightsFromFormFlow.
5. Drag the JSON to Object transformer from getFlightsFlow and drop it in the process section of getFlightsFromFormFlow.
6. Drag out a Flow Reference component from the palette and drop it in the process section of getFlightsFromFormFlow.
7. In the Flow Reference Properties view, set the flow name to getFlightsFlow.
Determine how to reference the web service destination and airline values
8. Return to mua.xml.
9. Add a Logger to the get:/flights/{destination} flow.
10. Add a breakpoint to the Set Payload transformer in the get:/flights/{destination} flow. 11. Save all the files and debug the application.
12. Make a request to http://localhost:8081/api/flights/LAX.
13. In the Mule Debugger view, look at the inbound properties;; you should see a query parameter called airline with a value of all.
Note: You may need to remove the breakpoints from the
14. Click the Variables tab;; you should see a variable called destination with a value of LAX.
15. Click the Resume button.
Call the backend flow
Call the backend flow
16. Return to the get:/flights/{destination} flow.
17. Add a Flow Reference component after the Set Payload transformer.
18. In the Flow Reference Properties view, set the flow name to getFlightsFlow.
Review the FlightRequest Java class
19. Open the com.mulesoft.training.FlightRequest.java class located in the project’s src/main/java folder and examine the code.
Set the payload to a Java object with flight request data
20. Double-click the Set Payload transformer in the get:/flights/{destination} flow.
21. In the Properties view, set the value to a new instance of com.mulesoft.training.FlightRequest, passing destination and message.inboundProperties.’http.query.params’.airline to it as arguments.
Note: You can also copy this expression form the course snippets.txt file.
Test the web service in the APIkit Consoles view
22. Save the files and run the application.
23. In the APIkit Consoles view, click the GET button for /flights/{destination}.
24. Click the Try It button.
25. Enter a destination of LAX (or any other value) and click the GET button;; you should now see the real data (for LAX) displayed.
26. Scroll up in the /flights/{destination} resource window.
27. Set the airline query parameter to united.
28. Click the GET button;; you see should now see only the flights for LAX on United.
Test the web service in a browser
29. Make a request to http://localhost:8081/api/flights/CLE; you should see data for all the flights to CLE returned.
thanks for posting its very helpfulmulesoft training
ReplyDeleteVery nice post with lots of information
ReplyDeleteMulesoft Online Training
Mulesoft Training in Hyderabad
ReplyDeleteThis information is really awesome thanks for sharing most valuable information.
Mulesoft Training in Hyderabad
Mulesoft Online Training
good and nice post with lots of information.
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course | Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Training in Pune | Certification | Online Training Course | Full Stack Training | Certification | Full Stack Online Training Course
ReplyDeleteThis is an amazing blog, thank you so much for sharing such valuable information with us.
Mule ESB Training
Learn Mulesoft Online
Mulesoft Training Courses
Mulesoft Online Course
Online Mulesoft Training
Mulesoft Certification
Learn Mulesoft
Mulesoft Online Training
Mule 4 Training
Mulesoft Training
Mule Training
Mule 4 Certification
I have found great and massive information on
ReplyDeleteWorkday payroll training
Workday financials training
Workday training
Python online training
Mulesoft training
Servicenow Online training
Java training
Salesforce training
Mulesoft Online Training
Latest Sarkari jobs
Off Campus Jobs
which helps you also. Thanks for Posting
Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing
ReplyDeleteMule 4 Training
Best Mulesoft Online Training
Great post thanks for posting
ReplyDeletemulesoft training in hyderabad
mulesoft training