如何从 mule 中的 Web 服务获取数据
how to get data from web service in mule
我正在使用 mule 公开 Web 服务。我已经构建了一个 WSDL 文件,我正在尝试从消息中获取文本以加载到数据库中,但我不知道如何获取文本。我正在使用 Logger 来记录我的有效负载,如下所示。
INFO 2015-01-20 11:00:54,470 [[ws-mule-cxf].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tra="http://training/">
<soapenv:Header/>
<soapenv:Body>
<tra:sayHi>
<!--Optional:-->
<ID>111</ID>
<!--Optional:-->
<name>John</name>
<!--Optional:-->
<industry>IT</industry>
<!--Optional:-->
<REF/>
</tra:sayHi>
</soapenv:Body>
</soapenv:Envelope>
所以基本上我想获取 ID、名称和行业节点中的文本值并将它们映射到数据库 table。任何人请让我知道如何编写代码来获取这些信息?谢谢!
现在我删除了数据库连接器部分,只放置了一组有效负载组件以从 header 和 body 中提取数据以检查 xpath 是否正常工作,但它不能。
配置XML在这里:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" 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" version="EE-3.5.2"
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/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="admin" password="pontiac" database="sqwarepeg" doc:name="MySQL Configuration"/>
<file:connector name="File" writeToDirectory="C:\Testing" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="sf2dbtestFlow1" doc:name="sf2dbtestFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<cxf:proxy-service port="SF2DBPort" namespace="http://training/" service="SF2DBService" payload="envelope" wsdlLocation="src/test/resources/SF2DB.wsdl" doc:name="CXF"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<set-payload value="#[xpath('//soapenv:Envelope/soapenv:Body/*')]" doc:name="Set Payload"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
错误信息是:
表达式 "xpath('//soapenv:Envelope/soapenv:Body/*')" 的执行失败。 (org.mule.api.expression.ExpressionRuntimeException)。消息负载的类型为:String
**更新:我添加了
<mulexml:namespace-manager includeConfigNamespaces="true">
<mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
<mulexml:namespace prefix="tra" uri="http://training/"/>
</mulexml:namespace-manager>
在 xml 中,现在它报告另一个错误:
INFO 2015-01-22 12:22:39,847 [[ws-mule-cxf].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: org.dom4j.tree.DefaultElement@4586a9c5 [元素: http: //训练/属性:[]/>]
WARN 2015-01-22 12:22:39,850 [[ws-mule-cxf].connector.http.mule.default.receiver.03] org.apache.cxf.phase.PhaseInterceptorChain:{http://training/}SF2DBService 的拦截器抛出异常,现在放松
org.apache.cxf.interceptor.Fault: 无法将 class org.dom4j.tree.DefaultElement 转换为 XMLStreamReader.
有人可以帮忙吗?谢谢!
**
您需要使用Database connector,看看它是如何工作的。您通常会使用参数化查询。并使用 MEL 的 XPath 支持。像这样:
<your-inbound-endpoint-here.../>
<set-variable variableName="id" value="#[xpath('//Body/sayHi/ID').text]"/>
<set-variable variableName="name" value="#[xpath('//Body/sayHi/name')]"/>
<set-variable variableName="industry" value="#[xpath('//Body/sayHi/industry')]"/>
<db:template-query name="insert">
<db:parameterized-query>
<![CDATA[
INSERT INTO YOUR_TABLE("ID", "NAME", "INDUSTRY") VALUES (:id, :name, :industry);
]]>
</db:parameterized-query>
<db:in-param name="id" defaultValue="#[id"/>
<db:in-param name="name" defaultValue="#[name]"/>
<db:in-param name="industry" defaultValue="#[industry]"/>
</db:template-query>
将 Http Inbound 与配置了 wsdl 详细信息的 SOAP 连接器结合使用。
我正在使用 mule 公开 Web 服务。我已经构建了一个 WSDL 文件,我正在尝试从消息中获取文本以加载到数据库中,但我不知道如何获取文本。我正在使用 Logger 来记录我的有效负载,如下所示。
INFO 2015-01-20 11:00:54,470 [[ws-mule-cxf].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tra="http://training/">
<soapenv:Header/>
<soapenv:Body>
<tra:sayHi>
<!--Optional:-->
<ID>111</ID>
<!--Optional:-->
<name>John</name>
<!--Optional:-->
<industry>IT</industry>
<!--Optional:-->
<REF/>
</tra:sayHi>
</soapenv:Body>
</soapenv:Envelope>
所以基本上我想获取 ID、名称和行业节点中的文本值并将它们映射到数据库 table。任何人请让我知道如何编写代码来获取这些信息?谢谢!
现在我删除了数据库连接器部分,只放置了一组有效负载组件以从 header 和 body 中提取数据以检查 xpath 是否正常工作,但它不能。
配置XML在这里:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" 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" version="EE-3.5.2"
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/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd">
<db:mysql-config name="MySQL_Configuration" host="localhost" port="3306" user="admin" password="pontiac" database="sqwarepeg" doc:name="MySQL Configuration"/>
<file:connector name="File" writeToDirectory="C:\Testing" autoDelete="false" streaming="true" validateConnections="true" doc:name="File"/>
<flow name="sf2dbtestFlow1" doc:name="sf2dbtestFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<cxf:proxy-service port="SF2DBPort" namespace="http://training/" service="SF2DBService" payload="envelope" wsdlLocation="src/test/resources/SF2DB.wsdl" doc:name="CXF"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<set-payload value="#[xpath('//soapenv:Envelope/soapenv:Body/*')]" doc:name="Set Payload"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
错误信息是:
表达式 "xpath('//soapenv:Envelope/soapenv:Body/*')" 的执行失败。 (org.mule.api.expression.ExpressionRuntimeException)。消息负载的类型为:String
**更新:我添加了
<mulexml:namespace-manager includeConfigNamespaces="true">
<mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
<mulexml:namespace prefix="tra" uri="http://training/"/>
</mulexml:namespace-manager>
在 xml 中,现在它报告另一个错误: INFO 2015-01-22 12:22:39,847 [[ws-mule-cxf].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: org.dom4j.tree.DefaultElement@4586a9c5 [元素: http: //训练/属性:[]/>] WARN 2015-01-22 12:22:39,850 [[ws-mule-cxf].connector.http.mule.default.receiver.03] org.apache.cxf.phase.PhaseInterceptorChain:{http://training/}SF2DBService 的拦截器抛出异常,现在放松 org.apache.cxf.interceptor.Fault: 无法将 class org.dom4j.tree.DefaultElement 转换为 XMLStreamReader.
有人可以帮忙吗?谢谢! **
您需要使用Database connector,看看它是如何工作的。您通常会使用参数化查询。并使用 MEL 的 XPath 支持。像这样:
<your-inbound-endpoint-here.../>
<set-variable variableName="id" value="#[xpath('//Body/sayHi/ID').text]"/>
<set-variable variableName="name" value="#[xpath('//Body/sayHi/name')]"/>
<set-variable variableName="industry" value="#[xpath('//Body/sayHi/industry')]"/>
<db:template-query name="insert">
<db:parameterized-query>
<![CDATA[
INSERT INTO YOUR_TABLE("ID", "NAME", "INDUSTRY") VALUES (:id, :name, :industry);
]]>
</db:parameterized-query>
<db:in-param name="id" defaultValue="#[id"/>
<db:in-param name="name" defaultValue="#[name]"/>
<db:in-param name="industry" defaultValue="#[industry]"/>
</db:template-query>
将 Http Inbound 与配置了 wsdl 详细信息的 SOAP 连接器结合使用。