目录

  • 1. 数据转换概念
  • 2. 数据智能感知 - DataSense
  • 3. 简单数据转换组件
    • 3.1 Object to JSON
    • 3.2 JSON to XML
    • 3.3 JSON to Object
    • 3.4 XML to JSON
  • 4. 企业版的DataWeave Transformer(可视化高级数据转换器)
  • 5. 社区版的InfoMapper(自研的可视化数据转换器)

本篇主要介绍在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="#[[&quot;orderNo&quot;:&quot;OST0001&quot;,&quot;orderAmount&quot;: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-005056c00001payload=org.mule.module.json.JsonDatacorrelationId=<not set>correlationGroup=-1correlationSeq=-1encoding=UTF-8exceptionPayload=<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 社区版 实施帮助。

欢迎转载,但必须保留原文和此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

转载于:https://www.cnblogs.com/enjoyingsoft/p/11294702.html

EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换相关推荐

  1. EnjoyingSoft之Mule ESB开发教程第二篇:Mule ESB基本概念

    目录 1. 使用Anypoint Studio开发 2. Mule ESB Application Structure - Mule ESB应用程序结构 3. Mule ESB Application ...

  2. iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面

    iOS 11开发教程(六)iOS11Main.storyboard文件编辑界面 在1.2.2小节中提到过编辑界面(Interface builder),编辑界面是用来设计用户界面的,单击打开Main. ...

  3. ArcGIS Maritime Server 开发教程(六)Maritime Service 开发技巧

    ArcGIS Maritime Server 开发教程(六)Maritime Service 开发技巧 本章导读:GIS 开发人员基于 Maritime Service 开发海图应用时总会遇到与海图数 ...

  4. Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler

    原文地址为: Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler 上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程( ...

  5. 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

    转:https://blog.csdn.net/forezp/article/details/70037291 最新版本: 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spr ...

  6. 1.1 haas506 2.0开发教程-导学篇

    haas506 2.0开发教程-导学篇 一. 下载并安装驱动 1.1 CH340 driver 1.2 8910 driver 二. 搭建开发环境 2.0 集合开发工具HaaS506PYcom 2.1 ...

  7. 微信公众帐号开发教程第6篇-消息及消息处理工具的封装

    本篇内容主要讲解如何将微信公众平台定义的消息及消息相关的操作封装成工具类,方面后期的使用  官方文档 接下来要做的就是将消息请求.回复中定义的消息进行封装,建立与之对应的Java类(Java是一门面向 ...

  8. Python开发【第六篇】:模块

    Python开发[第六篇]:模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一 ...

  9. 微信公众平台开发教程第22篇-如何保证access_token长期有效

    技术交流请加QQ群:Jeewx微信开发④[289709451] 为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用 ...

最新文章

  1. UIScrollView
  2. OC中创建对象,存入数组,并且遍历对象
  3. 技术人员如何创业《一》—— 产品及想法(转载)
  4. oracle对大对象类型操作:blob,clob,nclob,bfile
  5. ConcurrentHashMap的源码分析-initTable
  6. 修改onlyoffice存储为手动存储关闭浏览器时不进行保存
  7. html弹出保存文件对话框_有没有遇到过CAD文件损坏或打不开的情况?养成这个习惯很重要...
  8. centos8搭建nsis交叉编译环境
  9. dhclient常用命令
  10. 滑动窗口算法_从一道题讲解滑动窗口算法该如何实现
  11. 从零基础入门Tensorflow2.0 ----六、28 深度可分离卷积神经网络
  12. 航海王启航服务器维护要多久,《航海王启航》7月1日服务器更新维护公告
  13. vb是计算机语言吗,vb是什么语言?
  14. 一个变量对应多个标量的结果(平均值加标准差),Excel作图方法(多数据作图方法)
  15. 前端培训丁鹿学堂:vue3之计算数据和watch监听深入
  16. winform直接控制云台_比 2 代便宜的灵眸手机云台 3,竟然还多了 15 条新亮点!...
  17. 「WinddowInsets(二)」我们能用WindowInsets做什么?
  18. 15米 GMSLFPDLink传输线束降速问题
  19. Navicat Premium使用手册
  20. java从json数组中提取数据,从JSON数组中提取数据

热门文章

  1. Java 的面向接口编程
  2. Linux 命令之 ln -- 为文件创建链接
  3. Linux 命令之 tar 命令-打包和备份的归档工具(附压缩/解压工具)
  4. 本地 Windows 如何将 Web 工程部署到远程 Windows 主机上
  5. HH SaaS电商系统的商城模块设计
  6. 工作占用了太多私人时间_下班后还要被逼谈工作,我们应该如何处理?
  7. ubtunu打开firefox_Linux Ubuntu 终端命令行打开firefox报错
  8. Pytorch代码函数笔记
  9. python 创建空的numpy数组_数据分析-NumPy内置函数创建数组
  10. java 使按钮被选中_java – 让我的单选按钮在Android中被选中