This article is the second part of the last article Fetch MySQL Data Using Database Connector in MuleSoft
Getting Started
- First, open Anypoint Studio and Create a New Mule Project.
- Enter the project name and selection location and click finish.
- Drag and Drop Listener (HTTP) control from Mule palette to Message Flow, enter a display name, select Connector configuration, and Enter path with a query string value. In my sample, I am using {userId}.
- Next, drag and drop Database > Select control from Mule palette to Message follow and setup Display Name, Connector configuration, Write SQL query text and Input parameters.
- Next, drag and drop Logger from the Core palette and set a Generic message as Json Payload.
- Lastly, Drag and Drop Set Payload from the Core palette and setup the Payload value in Json format.
- That’s it. Now run the application.
- Open Postman and enter Endpoint to see the output result.
Validate If No Data?
- What is the best way to deal if no data exists for a given ID like this?
- Drag and drop the Choice (Core) component from the Mule palette to massage flow before the Set payload.
- Give Choice a name and set When expression “!isEmpty(payload)”.
- Also, drag and drop the Set payload component inside of When expression and give a name to the payload.
- Next, drag and drop Set Payload inside Default and set the default message.
- Let’s run the application to see the result.
Conclusion
In this article, we learned how to setup a database connector to get MySQL database based on conditions and display results in Postman.