本篇主要介紹在Mule ESB中使用數據轉換。數據轉換是ESB最核心的功能,它消除了異構應用之間的技術差異,讓不同的應用服務協調運作,實現了不同服務之間的通訊。數據轉換或者說消息轉換,包括了數據結構,數據類型,數據內容的轉換等。
作為開源ESB產品中很成熟的平台,Mule ESB內置很多的消息轉換組件,比如Object to JSON,Object to XML,XML to JSON等,同時也支持使用自定義的Transformer來擴充自定義轉換邏輯。
另外Mule ESB企業版更擁有圖形化的消息轉換組件DataWeave,可以很靈活的進行數據的轉換,包括一對一,一對多,多對一的映射。我們在社區版上也研發了相應的擴展組件InfoMapper,做到類似DataWeave的強大功能。
Mule ESB起源於一個社區項目,經過十多年的發展,Mule ESB擁有眾多的企業客戶案例,2017年成功在紐交所上市。我們作為MuleSoft的重要合作伙伴,參與其中,使用Mule ESB企業版實施開發,或者Mule ESB社區版實施開發,幫助國內眾多的行業領先客戶成功上線Mule ESB集成項目。
Mule ESB的源代碼托管在GitHub上,英文文檔也非常豐富,Mule ESB的中文教程卻非常少,我們使用8篇Mule ESB的開發教程來幫助大家迅速上手Mule ESB開發。
- EnjoyingSoft之Mule ESB開發教程第一篇:初識Mule ESB
- EnjoyingSoft之Mule ESB開發教程第二篇:Mule ESB基本概念
- EnjoyingSoft之Mule ESB開發教程第三篇:Mule message structure - Mule message結構
- EnjoyingSoft之Mule ESB開發教程第四篇:Mule Expression Language - MEL表達式
- EnjoyingSoft之Mule ESB開發教程第五篇:控制消息的流向-數據路由
- EnjoyingSoft之Mule ESB開發教程第六篇:Data Transform - 數據轉換
- EnjoyingSoft之Mule ESB開發教程第七篇:SOAP Web Service的消費和創建
- EnjoyingSoft之Mule ESB開發教程第八篇:使用API構建Rest Service
1. 數據轉換概念
數據轉換是ESB平台的核心模塊,是解決異構系統通訊的必備組件。舉個例子,訂單系統暴露了一個Web Service,而我們的網上商城生成的訂單是Json或者Flat file格式,將JSON數據或者Flat file文本格式轉換成Web Service的XML格式,這個需求就可以通過ESB來實現。我們在第三篇講解了Mule Message的結構,回憶一下Mule Message的結構。
我們通常說的數據轉換就是對Mule Message中Payload做操作,改變數據類型或者數據結構,數據內容等。在做數據轉換之前,我們需要確定數據的源類型和目標類型,然后選擇合適的數據轉換組件。
2. 數據智能感知 - DataSense
DataSense是Mule最強大的功能之一,Anypoint Studio會主動獲取元數據類型和結構等信息,以幫助開發者在應用程序中准確的映射或者使用數據。
DataSense配合企業版的DataWeave可視化數據轉化組件,或者我們自研的社區版可視化數據轉化組件InfoMapper,能夠很大的加速應用集成的開發,方便開發者完成數據轉換和映射。本文后半段會介紹DataWeave和InfoMapper的強大之處。
DataSense舉例說明,如果您的應用程序連接到一個天氣預報的Web Service,那么DataSense會捕獲Web Service使用的數據類型和結構的信息,這些信息對開發者非常有用,它會展示這個Web Service期望的的入參是什么,出參是什么。
天氣預報WebService中規定的入參和出參如下圖:
Anypoint Studio中的Web Service Consumer組件,會清楚的顯示Expected Data,也就是期望的入參數據格式。如下圖Input頁簽,可以看到需要輸入theCityName的XML數據。
同時Web Service Consumer組件也會展示這個WebService的輸出參數是什么。如下圖Output頁簽。
3. 簡單數據轉換組件
Mule ESB內置了很多常用的簡單的數據轉換組件,從組件名稱就可以看到組件的適用范圍,源數據類型和目標數據類型。另外Mule ESB也支持自定義Transformer,通過Java或者其他腳本語言實現復雜的數據轉換邏輯。
3.1 Object to JSON
Object to JSON組件能夠將Java對象轉換成JSON格式,方便其他語言比如C#,Python解析和使用。常見的使用場景就是將Map對象轉換成JSON格式。
Mule UI Config:
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="object-to-json-flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<set-payload value="#[["orderNo":"OST0001","orderAmount":1000]]" doc:name="Set Map Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
</mule>
使用Postman訪問,就可以得到一個標准的JSON數據。
{
"orderAmount": 1000,
"orderNo": "OST0001"
}
3.2 JSON to XML
JSON轉換成XML是很常用的轉換操作。JSON和XML都是有層級關系的數據展示結構,JSON和XML具體的區別和使用場景請參考其他文章。這里需要強調是XML有一個唯一的根節點元素,而JSON根節點元素可能是一個對象,還可能是一個數組。使用Mule完成JSON to XML也很簡單。
Mule UI Config:
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="json2xml-flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<json:json-to-xml-transformer doc:name="JSON to XML"/>
</flow>
</mule>
使用Postman提交Json,Json數據如下:
{
"customer": {
"cellPhone": "13912340002",
"customerNo": "C0002",
"customerName": "Alibaba"
}
}
得到轉換之后的結果如下:
<?xml version='1.0'?>
<customer>
<cellPhone>13912340002</cellPhone>
<customerNo>C0002</customerNo>
<customerName>Alibaba</customerName>
</customer>
這里還是要再次強調一下,這個組件要求Json數據必須只有一個根節點。如果Json數據里有多個根節點,則數據會丟失。舉例如下:
有多個根節點元素的Json數據:
{
"cellPhone": "13912340002",
"customerNo": "C0002",
"customerName": "Alibaba"
}
轉換后的XML結果如下,可以看到只有第一個根元素被轉換成了XML。
<?xml version='1.0'?>
<cellPhone>13912340002</cellPhone>
3.3 JSON to Object
這個組件會將JSON數據轉成成一個Mule內置的Json對象,org.mule.module.json.JsonData。
Mule UI Config:
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="json2object-flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<json:json-to-object-transformer doc:name="JSON to Object"/>
<logger message="#[message]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
通過打印的日志可以看到,轉化后的payload是org.mule.module.json.JsonData。
org.mule.DefaultMuleMessage
{
id=4b3b1290-b293-11e9-9351-005056c00001
payload=org.mule.module.json.JsonData
correlationId=<not set>
correlationGroup=-1
correlationSeq=-1
encoding=UTF-8
exceptionPayload=<not set>
}
3.4 XML to JSON
正如2.2章節所說,XML和Json的根節點元素存在差異。而XML的根節點元素是唯一的,所以XML to JSON比較簡單。
Mule UI Config:
Mule XML Config:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="xml2json-flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<json:xml-to-json-transformer doc:name="XML to JSON"/>
</flow>
</mule>
使用Postman提交Xml,Xml數據如下:
<?xml version='1.0'?>
<customer>
<cellPhone>13912340002</cellPhone>
<customerNo>C0002</customerNo>
<customerName>Alibaba</customerName>
</customer>
轉化后的JSON數據如下,可以看到這里的Json是正確的。
{
"customer": {
"cellPhone": "13912340002",
"customerNo": "C0002",
"customerName": "Alibaba"
}
}
4. 企業版的DataWeave Transformer(可視化高級數據轉換器)
DataWeave是Mule企業版提供的可視化的數據換器,使用這個組件可以通過拖拽的操作完成數據的映射。從下圖可以看到,DataWeave配合DataSense,通過拖拽連線的方式,可以將Http的請求數據轉換成Web Service Consumer期待的Xml數據,非常好用方便。
5. 社區版的InfoMapper(自研的可視化數據轉換器)
上文提到的DataWeave是Mule企業版才提供的功能,社區版並不提供該組件。我們也可以通過擴展組件的方式達到類似的功能,我們自研的可視化轉換組件InfoMapper,其功能和DataWeave相當。
數據轉換是ESB很常用的功能和使用場景,使用Anypoint Studio中提供的簡單數據轉換組件,或者企業版的DataWeave,社區版我們自研的InfoMapper這些高級轉換組件,配合強大的DataSense,數據轉換是非常輕松的事情。
本文同步發文於EnjoyingSoft Blogs ,CSDN,簡書
訪問EnjoyingSoft 網站,獲取更多Mule ESB 社區版 實施幫助。
歡迎轉載,但必須保留原文和此段聲明,且在文章頁面明顯位置給出原文鏈接,否則保留追究法律責任的權利。