Restful API with MULE ESB

ESB :  Enterprise Service Bus, is a communication system between heterogenous software applications in a Service Oriented Architecture.Thus ESB guarantees agility and flexibility with regard to high-level protocol communication between applications(as shown in the pictures below).





Key benefits of using an ESB :
  • Scales from point-solutions to enterprise-wide deployment (distributed bus)
  • More configuration rather than integration coding
  • No central rules-engine, no central broker
  • Easy plug-in and plug-out and loosely coupling system

Key disadvantages :
  • Slower communication speed, especially for those already compatible services
  • Single point of failure, can bring down all communications in the Enterprise
  • High configuration and maintenance complexity
In this article, we will try to create and consume a REST web service using mule ESB.
Wait!!! what is MULE
"Mule is a lightweight enterprise service bus (ESB) and integration framework provided by MuleSoft. The platform is Java-based, but can broker interactions between other platforms such as .NET using web services or sockets."  - Wikipedia

Prerequisites:
  1. Anypoint Studio: is a very usefull IDE based on Eclipse. that proposes a powerful tools for systems interactions.(downlaod link)
  2. Postman : a chrome extension to test our API(download).

Lets Start :
- First download Anypoint studio, unzip-it and run the application(similar to Eclipse).
- Now open new -> project -> Mule project :

- From the palette, drag and drop an HTTP component, it would be our listener, click to configure it :




The  display name is the document's name on the flow, host is our server and finally the port, make sure of the port is not used by other applications.
What's going on??? 😳
It's simple, we told MULE to listen to all HTTP requests to localhost:8084.
Now drop the VAR componenent and add it next to HTTP, it will allow us catching "things" from the http request.
finally we configure it as a set variable to catch the path name from the http request:


Note that we used MULE Expression (MEL) to get the path from the http request :
"#[message.inboundProperties.'http.request.path']".
Then, we use a flow control component to redirect the request. in this case CHOICE is perfect (drop it next to VAR).
By using MEL again we gonna make a "when" Statement "then"  processing.
You can see if the path contains '/news/' the component REST will processed.
you can add many components as you need next to choice.

Add a 3 REST components next to CHOICE, then setup the class path for processing:


P.S: Default in CHOICE properties is mandatory.
in our case, if the path does not match any of the patterns in the list(CHOICE "WHEN"), it's going to log it with the component LOGGER, here is the LOGGER setup :


Now it's JAVA's time :
Create 3 model classes, then 3 interfaces with there implementations :
Here is it :

Models: 



 Interfaces : 

  Implementations :


JAX-RS : Java API for RESTful Web Services (JAX-RS) is a Java programming language API spec that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern.[1] JAX-RS uses annotations, introduced in Java SE 5, to simplify the development and deployment of web service clients and endpoints. - WIKIPEDIA

@Path : specifies the relative path for a resource class or method.
@GET : specify the HTTP request type of a resource.
@Produces : specifies the response Internet media types (used for content negotiation)
@PathParam : binds the method parameter to a path segment.

Here is the flow's result in XML :


Now RUN your project :


If everything is OK, you get this message in the console :

With POSTMAN, past the path and send the request :


You will get the list of books in JSON format.
You can do a lots of cool things with MULE :
  • Using SALESFORCE or SAP API. 
  • Instead of a static lists you can try to get data from a database or flat files.
  • Also make  FTP endpoint as entry  point instead of HTTP.
Hope you liked this tutorial.  😉

Commentaires

Enregistrer un commentaire

Posts les plus consultés de ce blog

Serialisation d'un objet JAVA en XML avec JAXB