Build a Mulesoft API Step By Step — Part 1
This blog will take guide you to build a Mulesoft API from scratch step by step. An end-to-end tutorial.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
Hi All, it has been sometimes I have not come up with any blog. Sorry about that. Today, I thought to write something for the beginners. So, let us get going. Now, MuleSoft claims itself to be the #1 integration and API platform. So, today I will write about a Step-By-Step guide to create a Mule API. This blog would be more for the beginners and those who would like to learn MuleSoft and will walk you through a step by step process of building an API using the various tools and applications provided by MuleSoft at your disposal. If you are already aware of some steps and want to skip some steps, feel free to do so.
I will build an API on a couple of tables in Maria DB. You can use any other backend of your wish with your changes.
So, without any further ado, let us get started with the API building.
Prerequisites
- Access to the Anypoint Platform. This is also called Cloudhub. You can create your training account for 2 months. You can get the access here <https://anypoint.mulesoft.com/login/signup>
- Anypoint Studio. I am using the latest version of 7.5. Download it here <https://www.mulesoft.com/lp/dl/studio>
- Understanding of YAML is good but not necessary.
Step 1: Define the API Contract using RAML
With the API First building approach, the first step in building an API is to create the contract. So, we will take our first step in building the API contract using RAML. MuleSoft supports both RAML and OAS.
To do this we will go to the Anypoint platform and Sign in.
Next, we go to the menu on the top left and select the Design Center.
Click on Create New > Create API specification.
Give a Name and leave other settings as is. We will name this as Mule Demo Database API.
Click Create API Specification.
You will reach the API specification page. Now, this is where we will build our API.
Now we do not want to create one monolithic RAML and put everything in there. Instead, I will create some folders and link the various files from there. So, we will create some folder and change some names, and this is what your explorer should look like:
Now I would start writing my RAML and as an when I have to create any additional files, I will create them. To start with I will create some common traits. These are properties that I would like to put to every endpoint in my API.
Create a file called common-traits.YAML inside the common folder with the following lines
handlesAllErrors
responses
500
description Error response to indicate internal API error.
body
application/json
example
error system_exception
description System Exception Occurred
403
description Authentication error response.
body
application/json
example
error invalid_client
description Invalid Client Id or Client Secret
clientIdRequired
headers
client_id
displayName Client ID
type string
client_secret
displayName Shared Secret
type string
I will also create a documentation file inside the documentation folder. This should be a file in markdown language. For now, I will write the file as below
xxxxxxxxxx
## Mule Demo Database API
### API Overview
This API is only meant for learning and cannot be used in any other way.
##### Copyright
anupam .com
At this point, my API in the design would look something like this.
Next, let us create some example files. Let us have 2 endpoints. One called /job and the other called /employee. I will create 4 Json in the example folder 2 objects and 2 arrays for each type. I am putting the JSON below, but this is on you as to what data you want.
Job.json
xxxxxxxxxx
{
"jobCode" : "J001",
"jobTitle" : "Principal",
"jobDescription" : "Principal",
"minQualification" : "Ph.D",
"empClass" : "Faculty",
"minSalary" : 140000,
"maxSalary" : 190000,
"flsaStatus" : "Salaried (Exempt)"
}
Jobs.json
xxxxxxxxxx
[{
"jobCode" : "J001",
"jobTitle" : "Principal",
"jobDescription" : "Principal",
"minQualification" : "Ph.D",
"empClass" : "Faculty",
"minSalary" : 140000,
"maxSalary" : 190000,
"flsaStatus" : "Salaried (Exempt)"
}]
Employee.json
xxxxxxxxxx
{
"empId" : "E00001",
"nationalIDNumber" : "111-11-1111",
"eName" : "Mr Jon Doe",
"addressLn" : "1 Area",
"addressCity" : "Atlanta",
"addressZIP" : "30303",
"addressState" : "GA",
"addressCountry" : "USA",
"phone" : "9999999999",
"gender" : "Male",
"birthDate" : "1977-01-19"
}
Employees.json
xxxxxxxxxx
[{
"empId" : "E00001",
"nationalIDNumber" : "111-11-1111",
"eName" : "Mr Jon Doe",
"addressLn" : "1 Area",
"addressCity" : "Atlanta",
"addressZIP" : "30303",
"addressState" : "GA",
"addressCountry" : "USA",
"phone" : "9999999999",
"gender" : "Male",
"birthDate" : "1977-01-19"
}]
Now we would need to create the schema in RAML format. I will create 2 schema files, one for the employee and one for the job. They would like as below:
Job.schema
xxxxxxxxxx
#%RAML 1.0 DataType
type: object
properties:
jobCode: string
jobTitle: string
jobDescription: string
minQualification: string
empClass: string
minSalary: number
maxSalary: number
flsaStatus: string
Employee.schema
xxxxxxxxxx
#%RAML 1.0 DataType
type: object
properties:
empId:
type: string
minLength: 5
nationalIDNumber:
type: string
minLength: 9
maxLength: 9
eName: string
addressLn: string
addressCity: string
addressZIP: string
addressState: string
addressCountry: string
phone: string
gender:
type: string
enum:
- Male
- Female
birthDate:
type: date-only
Finally coming back to my API, I will now create all the necessary endpoints.
In this case, I will have 4 endpoints, GET, and POST for each employee and job. You can add other endpoints like PUT, DELETE or PATCH, etc.
Once you are done with the API, it should look like this.
xxxxxxxxxx
#%RAML 1.0
title: Mule Demo Database API
version: 1.0
protocols: [ HTTPS ]
mediaType: application/json
traits: !include common/common-traits.yaml
baseUri: http://localhost:8081/api/
documentation:
- title: Mule Demo Database API.
content: !include documentation/doc.md
types:
employee: !include schema/employee.raml
job: !include schema/job.raml
/employee:
post:
is: [handlesAllErrors,clientIdRequired]
description: Create new Employee
body:
application/json:
example: !include example/employees.json
type: employee[]
responses:
200:
body:
application/json:
example:
status: success
/{empId}:
get:
is: [handlesAllErrors, clientIdRequired]
description: Get employee with employee Id.
responses:
200:
body:
application/json:
example: !include example/employee.json
type: employee
/job:
post:
is: [handlesAllErrors,clientIdRequired]
description: Create new Job
body:
application/json:
example: !include example/jobs.json
type: job[]
responses:
200:
body:
application/json:
example:
status: success
/{jobCode}:
get:
is: [handlesAllErrors, clientIdRequired]
description: Get Job by Job Code.
responses:
200:
body:
application/json:
example: !include example/job.json
type: job
Now, you can immediately, click on the Mocking Service at the Right top corner and your services are up and running. This is what you can call from the Postman or any other Rest Client.
This completes our step 1 which would now take us to the second step of importing this API into our Studio and starting to write the interface.
Step 2: Import the API into Anypoint Studio
As for Step 2, We will switch on the Anypoint Studio and select a workspace where all our Mule Project will be stored.
Now select Create a Mule Project or click on File > New > Mule Project.
Give the project a name. Note here you cannot put a name with space. I will use mule-demo-database-app. Now in the API Implementation window, select Import From Design Center.
Click the browse button and a popup window will come asking for your credentials. Give the credentials which you used to login to Cloudhub. Note that you can deploy this application later into another Cloudhub or customer. This does not make a connection to the API, it simply pulls down the API to your application.
This should take you to a Browse Window where you should be able to see all APIs created in the design center of this account.
Click on the API which you created and click OK. If there is no error in your API you should see a Green Tick mark saying you are importing the following API.
Now click Finish and your API will be scaffolded, and the Skeleton Application will be automatically created for you.
This would now help you in the implementation of your API as per your need. If just at this point, you do run your application, you should be able to test your application with the dummy data.
Click on Run > Run > Run as Mule Application.
Your application will be deployed in the Mule runtime embedded in the Anypoint Studio and you can go to a browser and go to the endpoint http://localhost:8081/console to test your application. You should see a screen like this where you can TEST your application. Note, for now, you can use anything in client id and secret since the application does not verify the client authentication.
Now let us start building the API Implementation to execute the back-end code for this API.
Step 3: Build the API Implementation
In this step, we would create the Application Implementation. Before moving ahead, I like to create 3 files in the code. Go to the folder src/main/mule. Right-click > New > Mule Configuration File and type "global". Same way create a file for employee and job each. The global file will hold all global configurations whereas the job and employee should hold implementation for each of the individual endpoints. Move the Global Elements HTTP Listener Config and Router into the global.xml file.
After this, your workspace should look like this.
Now I will create the connectivity to my back end Maria DB using a database connector. The database here is in AWS so, it will be accessible from my computer as well as from the cloudhub. Note that if you have a local database, you may not be able to access it from the cloudhub.
Following are the content of each file
Global.xml
xxxxxxxxxx
<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo"
xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
<http:listener-config name="api-httpListenerConfig">
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<apikit:config name="api-config" api="api.raml" outboundHeadersMapName="outboundHeaders" httpStatusVarName="httpStatus" />
<db:config name="Database_Config" doc:name="Database Config" doc:id="42b3d660-c98f-481f-a478-7b9f57978bfd" >
<db:my-sql-connection host="*.*.*.*" port="3306" user="****" password="***" database="demo" />
</db:config>
</mule>
Api.xml
xxxxxxxxxx
<mule xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:apikit="http://www.mulesoft.org/schema/mule/mule-apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/mule-apikit http://www.mulesoft.org/schema/mule/mule-apikit/current/mule-apikit.xsd ">
<flow name="api-main">
<http:listener config-ref="api-httpListenerConfig" path="/api/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:router config-ref="api-config" />
<error-handler>
<on-error-propagate type="APIKIT:BAD_REQUEST">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Bad request"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">400</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:METHOD_NOT_ALLOWED">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Method not allowed"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">405</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_ACCEPTABLE">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not acceptable"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">406</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:UNSUPPORTED_MEDIA_TYPE">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Unsupported media type"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">415</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
<on-error-propagate type="APIKIT:NOT_IMPLEMENTED">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Not Implemented"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">501</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="api-console">
<http:listener config-ref="api-httpListenerConfig" path="/console/*">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<apikit:console config-ref="api-config" />
<error-handler>
<on-error-propagate type="APIKIT:NOT_FOUND">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{message: "Resource not found"}]]></ee:set-payload>
</ee:message>
<ee:variables>
<ee:set-variable variableName="httpStatus">404</ee:set-variable>
</ee:variables>
</ee:transform>
</on-error-propagate>
</error-handler>
</flow>
<flow name="get:\employee\(empId):api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable variableName="empId">attributes.uriParams.'empId'</ee:set-variable>
</ee:variables>
</ee:transform>
<flow-ref doc:name="employeeGet" doc:id="20d66a4d-dcff-40d9-81a9-9ef640322b2a" name="employeeGet"/>
</flow>
<flow name="get:\job\(jobCode):api-config">
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core">
<ee:variables>
<ee:set-variable variableName="jobCode">attributes.uriParams.'jobCode'</ee:set-variable>
</ee:variables>
</ee:transform>
<flow-ref doc:name="jobGet" doc:id="c442aa02-8a16-4285-bc99-742b7fb24879" name="jobGet"/>
</flow>
<flow name="post:\employee:application\json:api-config">
<flow-ref doc:name="employeeInsert" doc:id="960c1677-7334-4b0e-91ff-31137ec865c7" name="employeeInsert"/>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
status: "success"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
<flow name="post:\job:application\json:api-config">
<flow-ref doc:name="jobInsert" doc:id="739a32c6-0e36-45fd-8f8c-9f8416d50c86" name="jobInsert"/>
<ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
status: "success"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
</flow>
</mule>
Employee.xml
xxxxxxxxxx
<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
<flow name="employeeInsert" doc:id="5265fa9b-6f54-4f7c-8cdd-87642ee588b0" >
<ee:transform doc:name="Transform Message" doc:id="d40b196b-fa52-4bc6-81b3-cbd838baf0ff" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
query: "insert into employee (empId,nationalIDNumber,eName,addressLn,addressCity,addressZIP,addressState,addressCountry,phone,gender,birthDate)
values (:empId,:nationalIDNumber,:eName,:addressLn,:addressCity,:addressZIP,:addressState,:addressCountry,:phone,:gender,:birthDate)",
body: payload map ( payload01 , indexOfPayload01 ) -> {
empId: payload01.empId,
nationalIDNumber: payload01.nationalIDNumber,
eName: payload01.eName,
addressLn: payload01.addressLn,
addressCity: payload01.addressCity,
addressZIP: payload01.addressZIP,
addressState: payload01.addressState,
addressCountry: payload01.addressCountry,
phone: payload01.phone,
gender: payload01.gender,
birthDate: payload01.birthDate
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<db:bulk-insert doc:name="Bulk insert" doc:id="0736fde4-aff5-405d-8d34-bd40f644baec" config-ref="Database_Config">
<db:bulk-input-parameters ><![CDATA[#[payload.body]]]></db:bulk-input-parameters>
<db:sql ><![CDATA[#[payload.query]]]></db:sql>
</db:bulk-insert>
<logger level="INFO" doc:name="Logger" doc:id="c30da949-db08-471a-bc57-65d9b5b7dcf2" message="Insert of Data Complete"/>
</flow>
<flow name="employeeGet" doc:id="8380dba3-0eb4-4aa9-bfb4-182e8cc8a689" >
<ee:transform doc:name="Transform Message" doc:id="61716d39-89f2-41b8-9b18-17f8945a2b76" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
"select * from employee where empId = '" ++ vars.empId ++ "'"]]></ee:set-payload>
</ee:message>
</ee:transform>
<db:select doc:name="Select" doc:id="9ef1e38a-e8d5-4740-9798-05e6634a852f" config-ref="Database_Config">
<db:sql ><![CDATA[#[payload]]]></db:sql>
</db:select>
<ee:transform doc:name="Transform Message" doc:id="9d48be5a-7a19-4adc-b4b6-e53e65ce7d21" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload[0] default {}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="935042a6-83b8-4ba6-9723-e61bd61ce515" message='#["Got Data for " ++ (payload.eName default "None")]'/>
</flow>
</mule>
Job.xml
xxxxxxxxxx
<mule xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<flow name="jobInsert" doc:id="e136c434-10be-4b46-8043-57344343ccaa" >
<ee:transform doc:name="Transform Message" doc:id="096c0ad4-3f35-4cb9-b180-1bcbe2f73061" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
query: "insert into job (jobCode,jobTitle,jobDescription,minQualification,empClass,minSalary,maxSalary,flsaStatus)
values (:jobCode,:jobTitle,:jobDescription,:minQualification,:empClass,:minSalary,:maxSalary,:flsaStatus)",
body: payload map ( payload01 , indexOfPayload01 ) -> {
jobCode: payload01.jobCode,
jobTitle: payload01.jobTitle,
jobDescription: payload01.jobDescription,
minQualification: payload01.minQualification,
empClass: payload01.empClass,
minSalary: payload01.minSalary,
maxSalary: payload01.maxSalary,
flsaStatus: payload01.flsaStatus
}
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<db:bulk-insert doc:name="Bulk insert" doc:id="8f2a4472-997e-45c9-8e21-1d6f39a4c4f4" config-ref="Database_Config">
<db:bulk-input-parameters ><![CDATA[#[payload.body]]]></db:bulk-input-parameters>
<db:sql ><![CDATA[#[payload.query]]]></db:sql>
</db:bulk-insert>
<logger level="INFO" doc:name="Logger" doc:id="dede0e83-c71b-443b-92ed-9d51fc74a1d0" message="Insert of Data Complete"/>
</flow>
<flow name="jobGet" doc:id="ff4dc4f5-8532-4493-a569-cbfd448e8da4" >
<ee:transform doc:name="Transform Message" doc:id="b8ee96a1-1fc7-48d2-80bb-bfae7b660b04" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
"select * from job where jobCode = '" ++ vars.jobCode ++ "'"]]></ee:set-payload>
</ee:message>
</ee:transform>
<db:select doc:name="Select" doc:id="201fdc74-0c7d-43bf-be23-296a49f1cfc5" config-ref="Database_Config">
<db:sql ><![CDATA[#[payload]]]></db:sql>
</db:select>
<ee:transform doc:name="Transform Message" doc:id="2ee13a23-eddd-47af-a57b-c1bac01e005b" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload[0] default {}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="370287ca-2405-470f-b414-e73158160a28" message='#["Got Data for " ++ (payload.jobTitle default "None")]'/>
</flow>
</mule>
Go ahead and again run the application and you should be able to go to the console in http://localhost:8081/console and you should be able to test all the endpoints that we created.
Step 4: Deploy the API into Cloudhub and Test
As the last step, we will deploy this application into Cloudhub and run the endpoint. There are various ways to deploy the application into Cloudhub like using the maven or CI/CD pipeline. But we will simply deploy the app using Studio.
It should ask you a couple of information. For now, we will just put the application name with a suffix for the environment and deploy the app.
We can now go to the Cloudhub to check the status of our app. Once it is deployed it should look something like this:
Click on the app name and you should see various details. We need the domain name as mentioned below in blue.
Navigate to a web browser and use the endpoint HTTP://<domainname>/console. This should take you to the same Console as above and you will be able to test out the application.
With this, we come to the end of this blog. If you see this page, it says the application is not secure, this is because we have used HTTP and not HTTPS as our listener. In my next blog, I will write how to use the make the application secure and get more out of API Manager.
For more blogs like this and the application that I built, please visit https://www.wedointegration.com/post.php?posturl=build_a_mulesoft_api_step_by_step_part_1. Also, you can find the Github link there.
Published at DZone with permission of Anupam Chakraborty. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments