建设一个收入支出持平的网站,长春互联网推广,wordpress 下一页 模板,世界杯网站开发目录 1. 数据转换概念2. 数据智能感知 - DataSense3. 简单数据转换组件3.1 Object to JSON3.2 JSON to XML3.3 JSON to Object3.4 XML to JSON4. 企业版的DataWeave Transformer(可视化高级数据转换器)5. 社区版的InfoMapper(自研的可视化数据转换器)本篇主要介绍在Mule ESB中使… 目录 1. 数据转换概念2. 数据智能感知 - DataSense3. 简单数据转换组件3.1 Object to JSON3.2 JSON to XML3.3 JSON to Object3.4 XML to JSON4. 企业版的DataWeave Transformer(可视化高级数据转换器)5. 社区版的InfoMapper(自研的可视化数据转换器) 本篇主要介绍在Mule ESB中使用数据转换。数据转换是ESB最核心的功能它消除了异构应用之间的技术差异让不同的应用服务协调运作实现了不同服务之间的通讯。数据转换或者说消息转换包括了数据结构数据类型数据内容的转换等。 作为开源ESB产品中很成熟的平台Mule ESB内置很多的消息转换组件比如Object to JSONObject to XMLXML 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 ESBEnjoyingSoft之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 Service1. 数据转换概念 数据转换是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 version1.0 encodingUTF-8?mule xmlns:jsonhttp://www.mulesoft.org/schema/mule/jsonxmlns:httphttp://www.mulesoft.org/schema/mule/http xmlnshttp://www.mulesoft.org/schema/mule/core xmlns:dochttp://www.mulesoft.org/schema/mule/documentationxmlns:springhttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi: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.xsdhttp:listener-config nameHTTP_Listener_Configuration host0.0.0.0 port8081 doc:nameHTTP Listener Configuration/flow nameobject-to-json-flowhttp:listener config-refHTTP_Listener_Configuration path/ doc:nameHTTP/set-payload value#[[quot;orderNoquot;:quot;OST0001quot;,quot;orderAmountquot;:1000]] doc:nameSet Map Payload/json:object-to-json-transformer doc:nameObject 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 version1.0 encodingUTF-8?mule xmlns:jsonhttp://www.mulesoft.org/schema/mule/json xmlns:httphttp://www.mulesoft.org/schema/mule/http xmlnshttp://www.mulesoft.org/schema/mule/core xmlns:dochttp://www.mulesoft.org/schema/mule/documentationxmlns:springhttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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.xsdhttp:listener-config nameHTTP_Listener_Configuration host0.0.0.0 port8081 doc:nameHTTP Listener Configuration/flow namejson2xml-flowhttp:listener config-refHTTP_Listener_Configuration path/ doc:nameHTTP/json:json-to-xml-transformer doc:nameJSON to XML//flow
/mule使用Postman提交JsonJson数据如下: {customer: {cellPhone: 13912340002,customerNo: C0002,customerName: Alibaba}
} 得到转换之后的结果如下: ?xml version1.0?
customercellPhone13912340002/cellPhonecustomerNoC0002/customerNocustomerNameAlibaba/customerName
/customer 这里还是要再次强调一下这个组件要求Json数据必须只有一个根节点。如果Json数据里有多个根节点则数据会丢失。举例如下: 有多个根节点元素的Json数据: {cellPhone: 13912340002,customerNo: C0002,customerName: Alibaba
} 转换后的XML结果如下可以看到只有第一个根元素被转换成了XML。 ?xml version1.0?
cellPhone13912340002/cellPhone 3.3 JSON to Object 这个组件会将JSON数据转成成一个Mule内置的Json对象org.mule.module.json.JsonData。 Mule UI Config: Mule XML Config: ?xml version1.0 encodingUTF-8?mule xmlns:jsonhttp://www.mulesoft.org/schema/mule/json xmlns:httphttp://www.mulesoft.org/schema/mule/http xmlnshttp://www.mulesoft.org/schema/mule/core xmlns:dochttp://www.mulesoft.org/schema/mule/documentationxmlns:springhttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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.xsdhttp:listener-config nameHTTP_Listener_Configuration host0.0.0.0 port8081 doc:nameHTTP Listener Configuration/flow namejson2object-flowhttp:listener config-refHTTP_Listener_Configuration path/ doc:nameHTTP/json:json-to-object-transformer doc:nameJSON to Object/logger message#[message] levelINFO doc:nameLogger//flow
/mule 通过打印的日志可以看到转化后的payload是org.mule.module.json.JsonData。
org.mule.DefaultMuleMessage
{id4b3b1290-b293-11e9-9351-005056c00001payloadorg.mule.module.json.JsonDatacorrelationIdnot setcorrelationGroup-1correlationSeq-1encodingUTF-8exceptionPayloadnot set
} 3.4 XML to JSON 正如2.2章节所说XML和Json的根节点元素存在差异。而XML的根节点元素是唯一的所以XML to JSON比较简单。 Mule UI Config: Mule XML Config: ?xml version1.0 encodingUTF-8?mule xmlns:jsonhttp://www.mulesoft.org/schema/mule/json xmlns:httphttp://www.mulesoft.org/schema/mule/http xmlnshttp://www.mulesoft.org/schema/mule/core xmlns:dochttp://www.mulesoft.org/schema/mule/documentationxmlns:springhttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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.xsdhttp:listener-config nameHTTP_Listener_Configuration host0.0.0.0 port8081 doc:nameHTTP Listener Configuration/flow namexml2json-flowhttp:listener config-refHTTP_Listener_Configuration path/ doc:nameHTTP/json:xml-to-json-transformer doc:nameXML to JSON//flow
/mule 使用Postman提交XmlXml数据如下: ?xml version1.0?
customercellPhone13912340002/cellPhonecustomerNoC0002/customerNocustomerNameAlibaba/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