如何从mule中的Web服务获取数据

时间:2015-01-20 00:12:12

标签: 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,Name和行业节点中获取文本值并将它们映射到数据库表。有谁请让我知道如何编写代码来获取这些信息?谢谢!

现在我删除了数据库连接器部分,只放置了一个有效负载组件来从头部和主体中提取数据,以检查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>

错误消息是:

执行表达式&#34; xpath(&#39; // soapenv:Envelope / soapenv:Body / *&#39;)&#34;失败。 (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:// training / attributes:[] /&gt;] 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:无法将类org.dom4j.tree.DefaultElement转换为XMLStreamReader。

任何人都可以提供帮助吗?谢谢! **

2 个答案:

答案 0 :(得分:1)

您需要使用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>

答案 1 :(得分:0)

使用带有SOAP连接器的Http Inbound,并配置了wsdl详细信息。