In the previous tutorial, I already introduced to you all how to configure the HTTP Listener Connector in Anypoint Studio for Mule ESB application. In this tutorial, I will guide you all how to use it by an example.
My example is: we need a Mule ESB application that can receive an HTTP POST request, then get the body data from the request to transform it to other data format. To simply the example, we can log this body data request into the console.
First of all, we need create new Mule ESB Maven project as below:
To enable our Mule ESB application can receive the HTTP request from client, we need use HTTP Connector in the Message Source of Mule Flow.
You can find out the HTTP Connector in Mule Palette tool:
Let drag and drop it into the Editor of Anypoint Studio.
To configure for HTTP Connector, you can reference again previous tutorial.
Here, I configured it as below:
With Global HTTP Listener Configuration:
After receiving the request, we need to get the data from request body by using a Transformer component named Byte Array to String.
The Transformer component as calling name, it will get the byte data and convert them to string.
To add this component, in Mule Palette, let search “Byte Array to String”:
then drag and drop into Editor of Anypoint Studio.
With this transformer component, we don’t need to have any configuration for it. Everything from input and output for this component will be done automatically.
After converting the body request data to string, the payload of Mule ESB message will be this string.
The final step, we need to do that is: log the body request data into console using Logger component.
Let find out the Logger component in Mule Palette.
Drag and drop it into Editor
For this component, we only need configure to print out the payload from Mule message as below:
OK, we just finished the requirement. Now, we can make a test.
Let run our Mule ESB application, then open Postman tool and send a POST request with body data “name=Khanh”:
then check the result in the console.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
INFO 2017-08-23 17:17:16,704 [main] org.mule.module.launcher.StartupSummaryDeploymentListener: ********************************************************************** * - - + DOMAIN + - - * - - + STATUS + - - * ********************************************************************** * default * DEPLOYED * ********************************************************************** ******************************************************************************************************* * - - + APPLICATION + - - * - - + DOMAIN + - - * - - + STATUS + - - * ******************************************************************************************************* * mule-esb-using-http-listener-connector * default * DEPLOYED * ******************************************************************************************************* INFO 2017-08-23 17:19:44,406 [[mule-esb-using-http-listener-connector].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: name=Khanh |