本篇主要介绍在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开发。

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-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 BlogsCSDN简书

访问EnjoyingSoft 网站,获取更多Mule ESB 社区版 实施帮助。

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

EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换的更多相关文章

  1. EnjoyingSoft之Mule ESB开发教程第三篇:Mule message structure - Mule message结构

    目录 1. 探索Mule Message结构 2. Mule Message的Payload 3. Mule Message的Property 4. Mule Message的Attachment 5 ...

  2. EnjoyingSoft之Mule ESB开发教程第四篇:Mule Expression Language - MEL表达式

    目录 1. MEL的优势 2. MEL的使用场景 3. MEL的示例 4. MEL的上下文对象 5. MEL的Variable 6. MEL访问属性 7. MEL操作符 本篇主要介绍Mule表达式语言 ...

  3. EnjoyingSoft之Mule ESB开发教程系列第五篇:控制消息的流向-数据路由

    目录 1. 使用场景 2. 基于消息头的路由 2.1 使用JSON提交订单的消息 2.2 使用XML提交订单的消息 2.3 使用Choice组件判断订单格式 3. 基于消息内容的路由 4. 其他控制流 ...

  4. EnjoyingSoft之Mule ESB开发教程第一篇:初识Mule ESB

    目录 1. Mule ESB基本介绍 2. Mule ESB社区版和企业版 3. Mule ESB常用场景 4. Mule ESB软件安装 客户端安装 服务端安装 5. 第一个Mule ESB应用- ...

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

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

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

    上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK>我们讲述了如何使用Senparc.Weixin.MP SDK ...

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

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

  8. [051] 微信公众平台开发教程第22篇-怎样保证access_token长期有效

    为了使第三方开发人员能够为用户提供很多其它更有价值的个性化服务,微信公众平台开放了很多接口,包含自己定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等,开发人员在调用这些接口时.都须要 ...

  9. spring cloud系列教程第六篇-Eureka集群版

    spring cloud系列教程第六篇-Eureka集群版 本文主要内容: 本文来源:本文由凯哥Java(kaigejava)发布在博客园博客的.转载请注明 1:Eureka执行步骤理解 2:集群原理 ...

随机推荐

  1. Codeforces 782B:The Meeting Place Cannot Be Changed(三分搜索)

    http://codeforces.com/contest/782/problem/B 题意:有n个人,每个人有一个位置和速度,现在要让这n个人都走到同一个位置,问最少需要的时间是多少. 思路:看上去 ...

  2. netty实现的RPC框架

    自己手撸了一个nettyRPC框架,希望在这里给有兴趣的同学们做个参考. 要想实现nettyrpc需要了解的技术要点如下: spring的自定义注解.spring的bean的有关初始化. 反射和动态代 ...

  3. Oracle修改字段类型报错:“ORA-01439:要更改数据类型,则要修改的列必须为空”

    在oracle修改user表字段name类型时遇到报错:“ORA-01439:要更改数据类型,则要修改的列必须为空”,是因为要修改字段的新类型和原来的类型不兼容. 如果要修改的字段数据为空时,则不会报 ...

  4. 串门赛: NOIP2016模拟赛——By Marvolo 丢脸记

    前几天liu_runda来机房颓废,顺便扔给我们一个网址,说这上面有模拟赛,让我们感兴趣的去打一打.一开始还是没打算去看一下的,但是听std说好多人都打,想了一下,还是打一打吧,打着玩,然后就丢脸了. ...

  5. java学习笔记(基础篇)—抽象与接口的区别

    抽象与接口的区别 一.抽象(abstract) 1. 抽象方法 1) 作用:定义规范 2) 抽象方法用来描述具有什么功能,但不提供实现. 3) 如果类中一个方法没有实现就要定义一个抽象方法. 2. 抽 ...

  6. ASP.NET Core系列(二):创建第一个.Net Core 项目

    前面讲过 .NET Core简介及开发环境安装,本章会讲一讲ASP.NET Core 2.0的项目结构,查看完整的ASP.NET Core系列文章:https://www.cnblogs.com/zh ...

  7. 使用ML-Agents Toolkit(0.5)训练游戏ai之游戏打包

    这篇文章介绍如何训练官方的一个例子3dball. 确保在此之前已经安装好训练环境可以参考下面的文章. https://www.cnblogs.com/pojdd/p/9804322.html 游戏打包 ...

  8. python 2.7 - 3.5 升级之路 (二) : 语法与类库升级

    背景 在上一篇博文中,我们为升级python 2 -> 3已经做了一些准备.在这篇中,我们将针对语法与类库这两个方面进行讨论. 关于语法 1. print 在python3中, print 已经 ...

  9. [leetcode] #213 House Robber II Medium (medium)

    原题链接 比子母题House Robber多了一个条件:偷了0以后,第n-1间房子不能偷. 转换思路为求偷盗[0,n-1)之间,以及[1,n)之间的最大值. 用两个DP,分别保存偷不偷第0间房的情况. ...

  10. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...