Building Restful API | CRUD Operations in Mule 4


CRUD Operations in Mule 4 using RAML.

RAML is designed in Design center of anypoint platform and imported in Mule flow

RAML:

#%RAML 1.0
title: Employee API

version: v1

/person:
  get:
    description: get data of all persons
    displayName: person
    responses:
      200:
        body:
          application/json:
            example:
              [{
                "id" : 1,
                "firstName" : "Rahul",
                "lastName" : "Singh",
                "city" : "Mumbai"
              }]
  post:
    description: insert data of persons
    displayName: person
    responses:
      201:
        body:
          application/json:
            example:
              {
                "data" : "Data is inserted successfully"
              }
  put:
    description: update data of persons
    displayName: person
    responses:
      204:
        body:
          application/json:
            example:
              {
                "data" : "Data is updated successfully"
              }
  delete:
    description: delete data of persons
    displayName: person
    responses:
      200:
        body:
          application/json:
            example:
              {
                "data" : "Data is deleted successfully"
              }                                  


We have generated flows from RAML:


Generated flows are connected by main flow by flow reference.
Main flows:


I have used mysql db. Configuration is done in DB connector and by placing mysql dependency in pom.xml.

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>



XML project code:



<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:db="http://www.mulesoft.org/schema/mule/db"
       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/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">
       <db:config name="Database_Config" doc:name="Database Config" doc:id="8356c5f6-f096-4f51-a401-2d155e19e090" >
              <db:my-sql-connection host="localhost" port="3306" user="root" password="" database="mule4" />
       </db:config>
       <flow name="test_ramlcrudFlow" doc:id="6e2beb02-ba34-428a-9ca1-398794a42930" >
              <db:select doc:name="Select" doc:id="1d316629-5b14-4c20-8c58-0e4c45646749" config-ref="Database_Config">
                     <db:sql >select * from info;</db:sql>
              </db:select>
              <ee:transform doc:name="Transform Message" doc:id="76c1fa8f-890a-42a3-876d-4342c77e97b7" >
                     <ee:message >
                           <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
                     </ee:message>
              </ee:transform>
       </flow>
       <flow name="test_ramlcrudFlow1" doc:id="0528def8-b7e3-4306-b3ef-1c24650416d3" >
              <foreach doc:name="For Each" doc:id="e8c3faa7-92e2-4aa6-9ac1-c1b75ccd38d3" >
                     <db:insert doc:name="Insert" doc:id="027de1aa-9b8c-43e9-a0b7-f6c9b82174dd" config-ref="Database_Config">
                     <db:sql>insert into info(ID,NAME) values(:id,:name);</db:sql>
                     <db:input-parameters><![CDATA[#[{
"id" :payload.id,
"name" :payload.name
}]]]></db:input-parameters>
              </db:insert>
              </foreach>
              <set-payload value="#[%dw 2.0
output application/json
---
{
data:'Data is successfully inserted'
}]" doc:name="Set Payload" doc:id="5b4d03ea-620d-4e79-90ec-e4ba6202fef7" />
       </flow>
       <flow name="test_ramlcrudFlow2" doc:id="0825e214-79a3-41cb-b8b9-cc6d9760400d" >
              <foreach doc:name="For Each" doc:id="a7719b11-0510-4591-ad4f-3df4347c1fa2" >
                     <db:update doc:name="Update" doc:id="fdf20631-8b7c-492c-96c8-f8ce5d0353fb" config-ref="Database_Config">
                     <db:sql>update info set ID=:id,NAME=:name where ID=:id</db:sql>
                     <db:input-parameters><![CDATA[#[{'id':payload.id,
       'name':payload.name
}]]]></db:input-parameters>
              </db:update>
              </foreach>
              <set-payload value="#[%dw 2.0
output application/json
---
{
data:'Data is successfully updated'
}]" doc:name="Set Payload" doc:id="e7474e0b-6bec-41b9-8ebb-39e3a22f0b08" />
       </flow>
       <flow name="test_ramlcrudFlow3" doc:id="35d57845-fd2b-4f43-a975-73fa49d694b8" >
              <foreach doc:name="For Each" doc:id="c3436404-7017-4a71-b787-7f019ec5e28c" >
                     <db:delete doc:name="Delete" doc:id="93de87a5-e4ef-48d5-b9b6-cc33a897723f" config-ref="Database_Config">
                     <db:sql>delete from info where ID=:id;</db:sql>
                     <db:input-parameters><![CDATA[#[id:payload.id]]]></db:input-parameters>
              </db:delete>
              </foreach>
              <set-payload value="#[%dw 2.0
output application/json
---
{
data:'Data is successfully deleted'
}]" doc:name="Set Payload" doc:id="457fd4a7-851c-479e-95a6-fd7c6db3cdc0" />
       </flow>
</mule>


© 2020 goformule.com

Share on :

Mule 4:

XML to JSON in mule 4 Web service consumer VM Validation in Mule 4 Until Successful
Sub flow Set Variable & Remove Variable Set Transaction ID Scatter Gather Round Robin
Consume Restful Service CRUD in Mule 4 Parse Template Object to JSON Load Static Resource
JSON to XML Invoke Idempotent Filter ForEach Flat file to JSON
Fixwidth to JSON First Successful File Execute Error Handling
Email Dynamic Evaluate Custom Business Event CSV to JSON Copybook to JSON
Choice Router Async RabbitMQ


Mule 3:


Database Connector Async CXF Attachement XSLT
Mongo DB Cache scope Custom Business Event Gzip Compress Expression
SMTP For Each Expression Javascript Filter Reference
Salesforce Message Enricher Groovy Parse Template Idempotent
VM Poll Invoke Ruby Message
Webservice consumer Python Transformer reference Message Property Not
Collection Aggregator First Sucessful Catch Exception Strategy Reference Exception Strategy Or
Choice Message Chunk Aggregator Choice Exception Strategy Global Exception Strategy Schema Validation
Custom Aggregator Scatter Gather Custom Exception Strategy Batch Processing MEL
M Unit RAML Map Operator API Mocking Dataweave