RESTful APIs With Anypoint Studio and RAML
HTTP connectors play a major role in Mule ESB for REST implementation. The API Kit provided by Anypoint Studio helps you create a Mule flow from RAML definitions.
Join the DZone community and get the full member experience.
Join For FreeRAML (Rest API Modeling Language) is used for defining APIs. API kits are used to generate Mule flows from RAML definitions.
RESTful services use HTTP inbound endpoints in the Mule flow that returns a value.
APIs and REST are closely related — APIs are developed in REST in Anypoint Studio.
HTTP connectors play a major role in Mule ESB for REST implementation. It's recommended to use RAML for API definitions. The API Kit provided by Anypoint Studio helps you create a Mule flow from RAML definitions.
Here's a sample flow.
1. Use Anypoint Designer to Write RAML Definition
Below is the sample API definition created in RAML.
# % RAML 0.8
title: OrgMgmt
version: V1
baseUri: http: //localhost:8080/api
/companies:
displayName: Companies
get:
description: get the company names from collection.
queryParameters:
city:
type: string
required: false
example: bangalore
responses:
200:
body:
application / json:
example: !include examples / companies - example.json
title
: API title.version
: Version of the API.baseUri
: REST URI exposed to external parties for consuming.
In below example, define one GET
method that retrieves the company names from the collection object.
Note: The included sample companies details are in the companies-example.json
file.
2. Generate Flow in Anypoint Studio With API Kit
Note: In latest versions of Anypoint Studio, API Kit is preinstalled.
Create a project in Mule ESB and copy the RAML file in the resources folder.
Refer to the below screen:
3. Generate XML File
Right click on the RAML file, select MULE > Generate flows from API to generate the flow XML file as shown below:
<?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="api-httpListenerConfig" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<apikit:config name="api-config" raml="api.raml" consoleEnabled="false" doc:name="Router"/>
<flow name="api-main">
<http:listener config-ref="api-httpListenerConfig" path="/api/*" doc:name="HTTP"/>
<apikit:router config-ref="api-config" doc:name="APIkit Router"/>
<exception-strategy ref="api-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
</flow>
<flow name="api-console">
<http:listener config-ref="api-httpListenerConfig" path="/console/*" doc:name="HTTP"/>
<apikit:console config-ref="api-config" doc:name="APIkit Console"/>
</flow>
<flow name="get:/companies:api-config">
<set-payload value="[{
 "name": "Infosys",
 "id": "INFY",
 "homeCity": "Bangalore"
 }
 {
 "name": "ADPCompany",
 "id": "ADP",
 "homeCity": "hyderabad"
 } ]" doc:name="Set Payload"/>
</flow>
<apikit:mapping-exception-strategy name="api-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>
And you're done! Here's so extra helpful info:
API base URI: http://localhost:8080/api/.
Available method: /companies (
GET
method).Access the URL http://localhost:8080/api/companies.
Opinions expressed by DZone contributors are their own.
Comments