RAMLing Around With Mule ( Part 2 )
Check out this guide to RAMLing with Mule, focusing this time on how to use RAML in Anypoint, and testing our API.
Join the DZone community and get the full member experience.
Join For FreeIn the first part of this series, we discussed what RAML really is and how can we use this specification for designing, creating and testing the APIs for our RESTful services. If you are wondering where to find the first part, then here it is:
https://dzone.com/articles/ramling-around-with-mule
In this part, we will see how to use the RAML we created in our previous article with Mule ESB or simple in our Anypoint Studio.
To use an existing RAML file with our Mule application, we need to have APIKit plugins in our studio.
When we create a new APIKit project and click Finish, Anypoint Studio uses the imported RAML file to automatically generate skeletal backend flows, one for each resource-action pairing in the RAML file.
Also, Studio pre-populates each of the flows with one or more message processors which perform "mock" activities based on the content of the example response included with each resource-action pairing in the RAML file.
Reference: https://docs.mulesoft.com/anypoint-platform-for-apis/apikit-beyond-the-basics
How to Use RAML in Our Anypoint Studio???
We need to go back our API designer where we had created and tested our RAML. We need to click Export as shown below:
Please ensure the Mocking service button is off or else it will create Mock URL. We will see our RAML file is downloaded in our local system as a Zip file. We need to extract the file and use the .raml file generated.
Using RAML in Our Anypoint Studio
In Anypoint Studio, We create a new Mule project as below:
Let’s give the project name as TestRAML, and add APIKit component in the project and refer our RAML file from our local system, where we downloaded the file from API designer:-
Once we click finish button, we can see Mule has created the project and flows are automatically created based on our RAML file we created:-
If we go into the code, we will find the RAML file has created different types of flows and exception strategy with the APIKit :
If we see our code thoroughly we will find that the APIKit plugin has divided the flow into 2 parts as our RAML file described.It contains the main flow which exposes the API with the HTTP listener and the APIKit router route the request to flow called get:/books:api-config. Since, we only have one resource defined in our RAML, it is routed to only one flow created:
It also has an exception strategy where different exceptions are defined and is routed to that particular exception whenever any exception occurs.
Here you can see different exception defined and it is automatically generated by APIKit plugin:
Testing Our API
We will be running our Mule project as Mule application :-
So, to test the API, we can see APIKit Console has started:
If we don’t want to test our APIs there, then we can also test the APIs in a browser with the APIKit console. This console is provided by APIKit to test our APIs.For that we need to copy that URL and paste it in our browser directly:
We can see the APIKit console in our browser after putting the URL there. Now, we are ready to test our API in the browser with API console:
So, finally after filling the query param and then hitting the GET button we get our response:
Conclusion…
So we can see it is very very easy to create a RESTFul apis in our Anypoint Studio with a RAML file. With the help of APIKit plugin it gets very simple in designing and testing our APIs.
I hope you like this article on implementing RAML in our Anypoint Studio and I am looking forward to see your feedback in comment section.
Opinions expressed by DZone contributors are their own.
Comments