Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MuleSoft Dataweave Language: Reading Properties From Files Dynamically

DZone's Guide to

MuleSoft Dataweave Language: Reading Properties From Files Dynamically

This quick tutorial will show you how to read a properties file dynamically in Mule, using MuleSoft's Dataweave language.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements. What's below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

In this tutorial, we will see how we can use MuleSoft's Dataweave language to read a properties file dynamically.

As a first step, let's create a new Mule project using Anypoint Studio. After creating the project, let's define our code list as follows:

GB=United Kingdom
USA=United State of America
DE=Germany
JPN=Japan
CHN=China

We will name it 'codelist.properties.' Place it under your Mule project 'src/main/resouces' folder.

Image title

Next, let's add the properties file in our global configuration. We will add a new property place holder. For this step, go to Global Elements -> Create -> Property Place.

Image title

Let's create a flow with an HTTP inbound connector.

Image title

We have set the path to be '/country' and Allowed Methods to be 'GET' only. For HTTP Connector configuration, we have used the following settings:

Image title

As a next step, let us assume we will get a query parameter named 'code' in our request. We will set that parameter in a local variable named 'countryCode.'

Image title

In the final step, lets add a Transform Message component and call the dataweave function 'p.'

Image title

We can test the service after deploying the application in Anypoint Studio. You can use a browser or Postman to hit the service.

For request, try any one of the following URL's.

http://localhost:8081/country?code=GB

http://localhost:8081/country?code=USA

http://localhost:8081/country?code=DE

http://localhost:8081/country?code=JPN

The response will come back as follows (based on the code you've sent across as the query parameter):

{
  "country": "United Kingdom"
}

I hope this article helps you understand how to read properties file dynamically using the Mulesoft Dataweave language.

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:
mulesoft ,dataweave ,anypoint studio ,integration ,mule ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}