Design, Develop, Deploy and Manage a RESTful Application With MuleSoft
Learn how to create a RESTful application with MuleSoft from scratch based on two scenario.
Join the DZone community and get the full member experience.
Join For FreeThis article shows you how to design, develop, deploy, and manage a simple RESTful Application using MuleSoft.
Let us take a simple real-time scenario of how ATM works internally.
Scenario 1: /checkBalance?accountNum=12345&bankName=icici
When a user provides a valid account number and bank name, the API should result in the balance that is available. (Here we connect to Database to get details)
Scenario 2 : /withdrawBalance?accountNum=12345&bankName=icici&withDrawAmount=5000
When a user provides a valid account number, bank name and amount to be withdrawn, the API should result in the Total balance after deduction available. (Here we connect to Database to get details)
In both cases, we have to send Bad request if accountNumber is not exactly 5digits. And for now, let us have only 2 Banks icici and axis.
In scenario 2, if withDrawAmount is greater than the total balance available, return a statement saying Insufficient Funds.
Let us follow the following steps to build an API to achieve the scenarios.
Design
We have seen there is a scenario where we have certain validations regarding account number and bank name. To start off, we need to design a blueprint of our application which resembles the actual functionality. To design it, we use RAML (RESTful API Modeling Language).
RAML is a way of describing practically-RESTful APIs in a way that's highly readable by both humans and computers.
To make this happen, MuleSoft provides a space to Design - Anypoint Platform's Design Center.
Step1: To access Design Center go to http://anypoint.mulesoft.com/ GoTo DesignCenter.
Step2: Click on Design Center and click on Create --> Create API Specification
Step3: Design your RAML.
#%RAML 1.0
title BankDetails
version1.0
/checkBalance
get
queryParameters
accountNum
requiredtrue
maxLength5
minLength5
bankName
requiredtrue
enum'icici''axis'
responses
200
body
application/json
example
{"result":"your balance is 55000"}
/withDrawAmount
get
queryParameters
accountNum
requiredtrue
maxLength5
minLength5
bankName
requiredtrue
enum'icici''axis'
amountToBeWithdrawn
requiredtrue
responses
200
body
application/json
example
{'result':'your balance is 10000'}
Step 4: After designing your RAML, enable Mocking Service which appears on the top right of the Design Center. Then test your API Spec if it's working as expected.
Step 5: After testing the mock service, publish the spec to exchange (Click on Publish-> Publish to Exchange)
Step 6: Go to API Manager -> Manager API from Exchange, select the application you published in Exchange and click on Save. An API ID is generated for your application.
Develop
After writing RAML. We need to start developing the code based on it.
Step 1: For that. Goto Anypoint Studio. Create New Mule Project. Now Right-click on the project and Click Anypoint Platform --> Import from Design Center -->enter your anypoint platform credentials.
Step 2: You can see there are folders created under src/main/resources with folder names starting with API. You can see your raml under api folder
Step 3: Right-click on the project and click on Mule --> Generate Flows from REST API
This will create structured flows and half of the coding part in your flow. With API KitRouter (Which routes to that particular flow for the methods you defined in RAML or it goes to error handling if required values are not provided)
Deploy
Step 1: After completion of development. Don’t forget to Include “Auto-Discovery" Configuration where you should give API id value (explained above how to get this id ). And refer the flow to the main flow where APIkit Router is present.
Step 2: Now right-click on Project and run. Once you see your application is successfully deployed. Now copy the jar file generated under the target folder of your project.
Step 3: Now go to Anypoint Platform's Access Management. Click on the "Environments" tab, choose your env that you want your application to be deployed, get the client_id and client_secret values.
Step 4: Now to deploy Goto Anypoint Platform-->Runtime Manager -->Click on Deploy Application -->Choose File --> upload the jar file. Go to Runtime properties, add below properties:
anypoint.platform.client_id=********************************* anypoint.platform.client_secret=*********************************
Click on Deploy.
That’s it your application is deployed. You should be seeing Active status in the API manager of your application!
Now you URL is framed as below:
http:// <application url which you can see in runtime manager>/api/<your resource name>
Manage
If you want to apply any policies, Goto AnypointPlatform --> API Manager--> Select your application --> Add new policy and apply. Simple and easy
In this way, we can easily build our web service with less coding and more efficient performance. That's what Mule is all about!
Opinions expressed by DZone contributors are their own.
Comments