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 1.0
title: Employee API

version: v1

    description: get data of all persons
    displayName: person
                "id" : 1,
                "firstName" : "Rahul",
                "lastName" : "Singh",
                "city" : "Mumbai"
    description: insert data of persons
    displayName: person
                "data" : "Data is inserted successfully"
    description: update data of persons
    displayName: person
                "data" : "Data is updated successfully"
    description: delete data of persons
    displayName: person
                "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.


XML project code:

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

<mule xmlns:ee="" xmlns:db=""
       xmlns:doc="" xmlns:xsi="" xsi:schemaLocation="">
       <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" />
       <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>
              <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
       <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>
              <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 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>
              <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 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>
              <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" />

No comments:

Post a Comment

If you have any doubt, please let me know

© 2020

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

Converter Tools :

JSON to XML Converter XML to JSON Converter Decimal to Binary Decimal to Octal Binary to Decimal
Binary to Hexadecimal Binary to Octal Hexadecimal to Decimal Hexadecimal to Binary Hexadecimal to Octal
Octal to Decimal Octal to Binary Octal to Hexadecimal Number to Words Converter Text to Hex Converter
Hex to Text String to Binary Converter Binary to String Converter Word to HTML Converter Word to PDF Converter
JSON to YAML Converter CSV to JSON Converter XML to XSLT Converter XML to YAML Converter
JSON to CSV Converter JSON to TSV Converter CSV to TSV Converter CSV to XLS Converter

Image Tools :

PNG to JPG Converter PNG to BMP Converter PNG to GIF Converter JPG to PNG Converter JPG to BMP Converter
JPG to GIF Converter GIF to PNG Converter GIF to JPG Converter GIF to BMP Converter PNG to PDF Converter
JPEG to PDF Converter JPG to PDF Converter BMP to PDF Converter GIF to PDF Converter

String Utilities :

Reverse String Random String Generator Base64 Encoder Base64 Decoder Words Counter
Remove Accents Remove Duplicate Lines Remove Empty Lines Remove Line Breaks Remove Similar Lines
Sort Text Lines Reverse Text Lines Random Words Generator

Number Utilities :

Square Root Calculator Random Number Generator Area of Circle Calculator Date Calculator

Other Tools :

Find IP of Website Password Generator URL Encoder URL Decoder lorem ipsum Generator
IP to Hostname Port Checker JSON Escape JSON Unescape

Free Rest APIs for Testing :

GET Sample Rest APIs POST Sample Rest API PUT Sample Rest API PATCH Sample Rest API DELETE Sample Rest API
Login Sample Rest API User Registration Sample API User Registration Unsuccessful Sample Rest API Delay Response