Calling a Java function from DataWeave (and solving the deployment issue)
Today I needed to do a more complex transformation of a field in DataWeave: I needed to transform an object location from Degrees Minutes Seconds (DMS) to Decimal Degrees (DD). The calculation itself is not that hard if you are using a static Java method, but calling this method from my DataWeave transformation took a little more time/research than I hoped.
While writing this blog I’m using Mule 3.9.x and DataWeave version 1.0.
Declaring an Expression Language function
A little bit of Googling results in many hits (like this DZone article) that basically describe the steps that need to be done to call a Java function via an expression-language global-functions expression in Mule.
I created the Mule configuration element using the Create wizard on the Global Elements tab and accepted all default values in the wizard.
Then I went to the Configuration XML tab and manually added the expression-language block of code to the generated Configuration element. This resulted in the below Configuration element (please note that the http:config element was automatically generated by the creation wizard):
<configuration doc:name="Configuration">
<http:config useTransportForUris="false"/>
<expression-language>
<import class="nl.mikeheeren.convert.ConverterUtils"/>
<global-functions>
def dmsToDd(dms) {
return nl.mikeheeren.convert.ConverterUtils.dmsToDd(dms);
}
</global-functions>
</expression-language>
</configuration>
Calling the function from DataWeave causes a deployment error
Now that we have declared the function, it was time to actually use it. To test this I have created a simple Mule flow to transform a single (static) DMS string to separate latitude and longitude values:
%dw 1.0
%output application/json
---
dmsToDd("513231N0050400E")
However, when I tried to deploy the application, it failed due to the following error:
ERROR 2019-06-21 15:11:04,280 [main] org.mule.module.launcher.application.DefaultMuleApplication: null
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'expression-language'. One of '{"http://www.mulesoft.org/schema/mule/core":abstract-configuration-extension}' is expected.
Solving the deployment issue
It cost me more time than I hoped, but eventually it turned out that the fix for this issue is really simple: Just remove the http:config element that was automatically generated by Mule. This shouldn’t impact the rest of the flow, because the only configuration that is included here (useTransportForUris) also has false configured as it’s default value. This resulted in the following Configuration element declaration:
<configuration doc:name="Configuration">
<expression-language>
<import class="nl.mikeheeren.convert.ConverterUtils"/>
<global-functions>
def dmsToDd(dms) {
return nl.mikeheeren.convert.ConverterUtils.dmsToDd(dms);
}
</global-functions>
</expression-language>
</configuration>
After this was done I was able to deploy the application and successfully use the Java function:
Geen reacties
Geef jouw mening
Reactie plaatsenReactie toevoegen