<property> と <parameter> の標準データ型はプリミティブ型 (string、boolean、number、date、datetime、localdatetime、time、localtime、timezone、binary、any、regex) です。
プリミティブ型よりも複雑な構造の型を定義するには、モジュールに挿入するデータ型のカタログを作成します。この例では、次のコンテンツのカタログファイル (hello-smart-connector/smart-connector/src/main/resources/module-Hello-catalog.xml) を作成します。
<?xml version="1.0" encoding="UTF-8"?>
<catalogs xmlns="http://www.mulesoft.org/schema/mule/types" >
<catalog name="PersonXsdType" format="application/xml">
<schema format="application/xml+schema" location="./person-schema.xsd" />
</catalog>
<catalog name="PersonJsonType" format="application/json">
<schema format="application/json+schema" location="./person-schema.json" />
</catalog>
</catalogs>
このカタログファイルは XSD および JSON スキーマファイルを参照します。
tree hello-smart-connector/smart-connector フォルダーの構造は次のようになります。
➜ ~ tree hello-smart-connector/smart-connector
hello-smart-connector/smart-connector
├── pom.xml
└── src
└── main
└── resources
├── module-Hello-catalog.xml
├── module-Hello.xml
├── person-schema.json
└── person-schema.xsd
スキーマの準備ができたら、関連付けられたカタログ (PersonXsdType および PersonJsonType) を参照することで、定義された型を使用します。次に例を示します。
<module name="Hello XML SDK" prefix="module-hello" ... >
...
<operation name="person-xml-to-json" doc:description="Takes a Person in XML format and translates it to JSON">
<parameters>
<parameter name="content" type="PersonXsdType::{http://uri}Person"/>
</parameters>
<body>
<ee:transform>
<ee:set-payload><![CDATA[
%dw 2.0
%output application/json encoding='UTF-8'
---
{
"name" : vars.content.person.name,
"lastname" : vars.content.person.lastName,
"age" : vars.content.person.age as Number
}
]]></ee:set-payload>
</ee:transform>
</body>
<output type="PersonJsonType"/>
</operation>
<operation name="person-json-to-xml" doc:description="Takes a Person in JSON format and translates it to XML">
<parameters>
<parameter name="content" type="PersonJsonType"/>
</parameters>
<body>
<ee:transform>
<ee:set-payload><![CDATA[
%dw 2.0
%output application/xml
---
person : vars.content
]]></ee:set-payload>
</ee:transform>
</body>
<output type="PersonXsdType::{http://uri}Person"/>
</operation>
<module/>
JSON スキーマの type 属性の値が、そのスキーマ (PersonJsonType) を含むカタログの名前になっています。ただし、XML スキーマの場合、type 属性の値では 2 つのコロン :: と qname (修飾名) 参照が Person 要素に付加されます (PersonXsdType::{http://uri}Person)。
JSON から XML への DataWeave 変換 (<ee:transform/> 内で示されている) を実行するには、モジュールで必須スキーマ (mule-ee.xsd) を見つけることができるように次の連動関係を POM ファイルに追加する必要があります。
<dependency>
<groupId>com.mulesoft.mule.runtime.modules</groupId>
<artifactId>mule-module-spring-config-ee</artifactId>
<version>${mule.version}</version>
<scope>provided</scope>
</dependency>
上記の例の操作を Mule アプリケーションで使用するには、値を操作に提供する必要があります。次に例を示します。
<mule ...>
<flow name="person-xml-2-json-flow">
<!-- create an XML Person and store it in the payload -->
<ee:transform>
<ee:set-payload><![CDATA[
%dw 2.0
%output application/xml
---
person : {
name : "Lautaro",
lastName: "Fernandez",
age : 54
}
]]></ee:set-payload>
</ee:transform>
<!-- call the operation -->
<module-hello:person-xml-to-json content="#[payload]"/>
<!-- at this point, the payload is a JSON Person -->
</flow>
<flow name="person-json-2-xml-flow">
<!-- create a JSON Person and store it in the payload -->
<ee:transform>
<ee:set-payload><![CDATA[
%dw 2.0
%output application/json
---
{
name : "Lautaro",
lastName: "Fernandez",
age : 54
}
]]></ee:set-payload>
</ee:transform>
<!-- call the operation -->
<module-hello:person-json-to-xml content="#[payload]"/>
<!-- at this point, the payload is an XML Person -->
</flow>
</mule>
プリミティブ型ではない値をパラメーター化すると、定義済みの <operation> で値を role="CONTENT" として宣言できるため、<flow> 内で追加のプロセッサーを使用して操作をコールする必要がなくなります。この例の person-xml-to-json 操作でこの属性を content パラメーターに追加します。
<module name="Hello XML SDK" prefix="module-hello" ... >
...
<operation name="person-xml-to-json" doc:description="Takes a Person in XML format and translates it to JSON">
<parameters>
<parameter name="content" type="PersonXsdType::{http://uri}Person" role="CONTENT"/>
</parameters>
<body>
<ee:transform>
<ee:set-payload><![CDATA[
%dw 2.0
%output application/json encoding='UTF-8'
---
{
"name" : vars.content.person.name,
"lastname" : vars.content.person.lastName,
"age" : vars.content.person.age as Number
}
]]></ee:set-payload>
</ee:transform>
</body>
<output type="PersonJsonType"/>
</operation>
...
<module/>
上記の例の操作を Mule アプリケーションで使用するには、値を操作に提供する必要があります。次に例を示します。
<mule ...>
<flow name="person-xml-2-json-using-content-flow">
<!-- call the operation -->
<module-hello:person-xml-to-json>
</module-hello:content><![CDATA[
%dw 2.0
%output application/xml
---
person : {
name : "Lautaro",
lastName: "Fernandez",
age : 54
}]]>
</module-hello:content>
</module-hello:person-xml-to-json>
<!-- at this point, the payload is a JSON Person -->
</flow>
..
</mule>