Ramification in Mule (With Example)
This tutorial will show you how to generate Mule flows from a RAML file in Anypoint Studio, with step-by-step instructions and XML code.
Join the DZone community and get the full member experience.
Join For FreeRESTful API Modeling Language (RAML) - Write once. Use many. Creative laziness encouraged. RAML makes it easy to manage the whole API lifecycle from design to sharing. It's concise - you only write what you need to define - and reusable. Its machine-readable API design is actually human-friendly.
The Mule API Kit helps to build the APIs from Anypoint Studio using a RAML file. Please follow the below steps to generate flows from a RAML file:
Step 1: Create a new Mule project.
Step 2: In Anypoint Studio, Create a RAML file using the RAML API editor.
#%RAML 0.8
title: RAMLwithMULE
version: 1.0
baseUri: http://localhost:7777/api
/get:
get:
description: sample
responses:
200:
body:
text/plain:
example: This is get call, MULE flow from RAML
/show:
get:
description: sample
responses:
200:
body:
text/plain:
example: This is get call and name show, MULE flow from RAML
Step 3: Generate flows from RAML. Right-click on RAML file -> MULE-> Generate Flows from RAML.
Step 4: A Mule XML file will be generated based on the RAML file.
Flow XML image:
Flow XML:
<?xml version="1.0" encoding="UTF-8"?>
<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/apikit" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:spring="http://www.springframework.org/schema/beans" 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/apikit http://www.mulesoft.org/schema/mule/apikit/current/mule-apikit.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<http:listener-config name="test2-httpListenerConfig" host="0.0.0.0" port="7777" doc:name="HTTP Listener Configuration"/>
<apikit:config name="test2-config" raml="test2.raml" consoleEnabled="true" consolePath="console" doc:name="Router"/>
<flow name="test2-main">
<http:listener config-ref="test2-httpListenerConfig" path="/api/*" doc:name="HTTP"/>
<apikit:router config-ref="test2-config" doc:name="APIkit Router"/>
<exception-strategy ref="test2-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
</flow>
<flow name="get:/get:test2-config">
<set-payload value="This is get call, generated MULE flow from RAML" doc:name="Set Payload"/>
</flow>
<flow name="get:/show:test2-config">
<set-payload value="This is get call and name show, generated MULE flow from RAML" doc:name="Set Payload"/>
</flow>
<apikit:mapping-exception-strategy name="test2-apiKitGlobalExceptionMapping">
<apikit:mapping statusCode="404">
<apikit:exception value="org.mule.module.apikit.exception.NotFoundException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Resource not found" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="405">
<apikit:exception value="org.mule.module.apikit.exception.MethodNotAllowedException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Method not allowed" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="415">
<apikit:exception value="org.mule.module.apikit.exception.UnsupportedMediaTypeException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Unsupported media type" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="406">
<apikit:exception value="org.mule.module.apikit.exception.NotAcceptableException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Not acceptable" }" doc:name="Set Payload"/>
</apikit:mapping>
<apikit:mapping statusCode="400">
<apikit:exception value="org.mule.module.apikit.exception.BadRequestException" />
<set-property propertyName="Content-Type" value="application/json" doc:name="Property"/>
<set-payload value="{ "message": "Bad request" }" doc:name="Set Payload"/>
</apikit:mapping>
</apikit:mapping-exception-strategy>
</mule>
Run the project as a MULE application and access the API.
Two methods defined in the RAML file:
http://localhost:7777/api/get -output in the browser
This is a get call, generated MULE flow from RAML.
http://localhost:7777/api/show -output in the browser
This is a get call and method name show, generated MULE flow from RAML.
Opinions expressed by DZone contributors are their own.
Comments