Create RESTful Web Service using RAML and APIkit Router in Mule ESB

In the previous tutorial, I introduced to you all about RAML to define APIs for RESTful Web Service. The APIkit Router is a function of Mule ESB that allows us to create new RESTful Web Service from a RAML file. How is it in details? Let’s find out in this tutorial.

For example, I have a RAML file defining the APIs for a student management system as follows:

Based on the above RAML file, you can see that my student management system allows users to query the list of students, find student by ID, or delete student by ID.

OK, now we go to the main thread of this tutorial.

First, I will create a new Mule project using above RAML file.

In Anypoint Studio IDE, go to File, select New and choose Mule Project:

Create RESTful Web Service using RAML and APIkit Router in Mule ESB

Just fill in the Project Name as usual.

In this window, as you can see, we have a section called APIkit Settings. This section allows us to use the RAML file with the Mule ESB APIkit Router to generate the Mule Flow for each request URL that we have defined in the RAML file. You will see what I just said at the moment, now, just check Add APIkit components and then select the RAML file as follows:

Create RESTful Web Service using RAML and APIkit Router in Mule ESB

Select the RAML file and click the Finish button to finish creating new Mule project.

My result is as follows:

Create RESTful Web Service using RAML and APIkit Router in Mule ESB

As you can see in the src/main/api directory, the RAML file which we just used to create the new project.

Now, you open the Mule Configuration File student.xml file, you will see, Mule ESB has created new Mule Flows using APIkit Router based on the definition of the RAML file automatically. The 3 request URLs that I defined in the RAML file, were generated to 3 Mule Flows as follows:

Create RESTful Web Service using RAML and APIkit Router in Mule ESB

Our task now is to simply add processing to complete our APIs. By default, Mule will respond to the response body that we defined in the example for each request URL in our RAML file. You can see the endpoint Set Payload to see this clearly. Here, I will not implement more processing for each request URL, you can add the processing according to your needs.

In addition, the Mule ESB Router API also supports generating HTTP status codes for us in the event of a failure in processing our request URLs.

Create RESTful Web Service using RAML and APIkit Router in Mule ESB

You also need to handle your needs accordingly.

Now, if you run this project, you will see a tab of the APIkit Console opened in Anypoint Studio as follows:

Create RESTful Web Service using RAML and APIkit Router in Mule ESB

This is the function of Mule ESB in Anypoint Studio that allows us to experience the APIs we have defined in the RAML file. As you can see, all the APIs shown here, you can use it to check the APIs which we define in the RAML file fit your needs or not? From that, you can have the adjustments accordingly.

4/5 - (3 votes)

Add Comment