Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

In this blog post we discuss a simple example created to demonstrate the use of RAML and mule-module-requester. RAML is a tool that allows for describing RESTful APIs. Using RAML, we may define a REST API and generate Mule flows that will route requests to the backend logic for the REST service. In this example we create a RESTful image file hosting application. The client will be able to request a list of image files as a JSON array and request an image file to download.

The mule-module-requester is a module that can be used inside a Mule flow to make a request midway through the flow. This module is best suited to use with transports that do not support a request-response exchange pattern such as FTP, File transport and so on. Such transports typically do not allow for requesting a resource on demand. In this example we shall request a File resource on demand.

In order to generate the RESTful Mule application using RAML, we need to create a ‘.raml’ file to describe each action. The ‘GET /images’ flow will return a JSON array containing the path of each image residing on the server. The ‘GET /images/{image}’ flow returns ‘image.jpeg’ where ‘image’ is the last part of the URL request; this is conveniently copied to the invocation properties to be used in the flow logic.

In this example we set the global return type in the raml file to “application/json”. The “get images” operation will return a JSON array. The “get image” operation returns a file, therefore we override the return type to; “*/*” indicating that the content type is to be set from within the Mule flow instead. Setting the content type to “image/jpeg” from the raml file for this operation did not render the image well from the browser, setting it manually from the flow did the trick.

Below is the flow structure generated by the above raml file, we are required to add the backend logic to the flows accordingly (Note: this extra configuration has been omitted from the flow below):

For each operation defined in the RAML file we are able to define a schema and example to be used to verify what the response should contain. The JSON examples for ‘image’ and ‘images’ shown below were used to generate xsd schemas using this schema generator:

The flow ‘get:/images:test-config’ calls a custom component that retrieves the files from a folder (GetImageListComponent.java) and a custom transformer (ListToMap.java) that converts the list of files into a Map of Maps containing the path to each image file. The “json:object-to-json-transformer” transforms this map into JSON.

The ‘get:/images/{image}:test-config’ flow will retrieve a file with name taken from the invocation property “image”.

I hope you have enjoyed this blog post (first published on; blog.ricston.com).