Over a million developers have joined DZone.

RESTful APIs With Anypoint Studio and RAML

DZone's Guide to

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.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

RAML (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

displayName: Companies
 description: get the company names from collection.
 type: string
required: false
example: bangalore
 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:

Image title

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"?>
	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="" 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 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 name="get:/companies:api-config">
		<set-payload value="[{&#xA;                &quot;name&quot;: &quot;Infosys&quot;,&#xA;                &quot;id&quot;: &quot;INFY&quot;,&#xA;                &quot;homeCity&quot;: &quot;Bangalore&quot;&#xA;              }&#xA;              {&#xA;                &quot;name&quot;: &quot;ADPCompany&quot;,&#xA;                &quot;id&quot;: &quot;ADP&quot;,&#xA;                &quot;homeCity&quot;: &quot;hyderabad&quot;&#xA;              }  ]" doc:name="Set Payload"/>
	<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="{ &quot;message&quot;: &quot;Resource not found&quot; }" doc:name="Set Payload"/>
		<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="{ &quot;message&quot;: &quot;Method not allowed&quot; }" doc:name="Set Payload"/>
		<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="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" doc:name="Set Payload"/>
		<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="{ &quot;message&quot;: &quot;Not acceptable&quot; }" doc:name="Set Payload"/>
		<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="{ &quot;message&quot;: &quot;Bad request&quot; }" doc:name="Set Payload"/>

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.

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

mule ,anypoint ,raml ,rest ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}