{{announcement.body}}
{{announcement.title}}

Design, Develop, Deploy and Manage a RESTful Application With MuleSoft

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

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

YAML
 




x
79


 
1
#%RAML 1.0
2
 
          
3
title: BankDetails
4
 
          
5
version: 1.0
6
 
          
7
/checkBalance:
8
 
          
9
 get:
10
 
          
11
   queryParameters:
12
 
          
13
     accountNum:
14
 
          
15
       required: true
16
 
          
17
       maxLength: 5
18
 
          
19
       minLength: 5
20
 
          
21
     bankName:
22
 
          
23
       required: true
24
 
          
25
       enum: ['icici','axis']
26
 
          
27
   responses:
28
 
          
29
     200:
30
 
          
31
       body:
32
 
          
33
         application/json:
34
 
          
35
           example: |
36
 
          
37
             {"result":"your balance is 55000"}
38
 
          
39
 
          
40
 
          
41
 
          
42
/withDrawAmount:
43
 
          
44
 get:
45
 
          
46
   queryParameters:
47
 
          
48
     accountNum:
49
 
          
50
       required: true
51
 
          
52
       maxLength: 5
53
 
          
54
       minLength: 5
55
 
          
56
     bankName:
57
 
          
58
       required: true
59
 
          
60
       enum: ['icici','axis']
61
 
          
62
     amountToBeWithdrawn:
63
 
          
64
       required: true
65
 
          
66
 
          
67
 
          
68
 
          
69
   responses:
70
 
          
71
     200:
72
 
          
73
       body:
74
 
          
75
         application/json:
76
 
          
77
           example: |
78
 
          
79
             {'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 2You 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!

Topics:
mule 4, mule 4 tutorial, mule application tutorial, mulesoft, mulesoft integration, mulesoft tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}