DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Trending

  • Top Load Balancing Algorithms: Choosing the Right Strategy
  • Are Traditional Data Warehouses Being Devoured by Agentic AI?
  • Load Testing Essentials for High-Traffic Applications
  • The Shift to Open Industrial IoT Architectures With Data Streaming
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. RESTful APIs With Anypoint Studio and RAML

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.

By 
Rajesh Kumar user avatar
Rajesh Kumar
·
Mar. 24, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
25.1K Views

Join the DZone community and get the full member experience.

Join For Free

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

 /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:

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"?>
<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="[{&#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"/>
	</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="{ &quot;message&quot;: &quot;Resource not found&quot; }" 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="{ &quot;message&quot;: &quot;Method not allowed&quot; }" 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="{ &quot;message&quot;: &quot;Unsupported media type&quot; }" 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="{ &quot;message&quot;: &quot;Not acceptable&quot; }" 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="{ &quot;message&quot;: &quot;Bad request&quot; }" 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.

REST Web Protocols

Opinions expressed by DZone contributors are their own.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: