icon-arrow icon-check icon-mail icon-phone icon-facebook icon-linkedin icon-youtube icon-twitter icon-cheveron icon-download icon-instagram play close close icon-arrow-uturn icon-calendar icon-clock icon-search icon-chevron-process icon-skills icon-knowledge icon-kite icon-education icon-languages icon-tools icon-experience icon-coffee-cup
Werken bij Integration & Application Talents
Blog 25/06/2019

Calling a Java function from DataWeave (and solving the deployment issue)

Transformation

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.

Whitehorses
Mike Heeren /
Integratie expert

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.

Anypoint Studio - Create Configuration

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:

Postman showing working DataWeave Java transformation

Geen reacties

Geef jouw mening

Reactie plaatsen

Reactie toevoegen

Jouw e-mailadres wordt niet openbaar gemaakt.

Geen HTML

  • Geen HTML toegestaan.
  • Regels en alinea's worden automatisch gesplitst.
  • Web- en e-mailadressen worden automatisch naar links omgezet.
Whitehorses
Mike Heeren /
Integratie expert

Wil je deel uitmaken van een groep gedreven en ambitieuze experts? Stuur ons jouw cv!