【问题标题】:how to get data from web service in mule如何从 mule 中的 Web 服务获取数据
【发布时间】:2015-01-20 00:12:12
【问题描述】:

我正在使用 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、名称和行业节点中的文本值,并将它们映射到数据库表。任何人都请让我知道如何编写代码来获取这些信息?谢谢!

现在我已经删除了数据库连接器部分,只放置了一个有效负载组件来从标题和正文中提取数据以检查 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)。消息负载的类型:字符串

**更新:我添加了

  <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中,现在它报告另一个错误: 信息 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/ 属性:[]/>] 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。

有人可以帮忙吗?谢谢! **

【问题讨论】:

  • 您似乎在使用 Mule 来使用远程 Web 服务,而不是公开 Web 服务。对吗?
  • 显示帮助流程。
  • 谢谢,但是我要暴露一个web服务,而不是消费

标签: mule


【解决方案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>

【讨论】:

  • 谢谢,这很有帮助。但我发现我不能使用 xpath。我的流程包含以下组件:HTTP、CXF、DOM TO XML、SetPayload、DataMapper 和数据库。在 SetPayload 组件中,我写:“#[xpath('soapenv:Envelope/soapenv:Body/*')]”,但它抛出一个错误说:执行表达式“xpath('soapenv:Envelope/soapenv:Body/ *')“ 失败的。 (org.mule.api.expression.ExpressionRuntimeException)。消息负载的类型:字符串
  • 我只是把我的流程做一个简单的版本,只包括这个:HTTP、CXF、DOM to XML、Variable、Logger。在变量中,我用“#[xpath('//Body/sayHi/ID').text]”设置了一个变量,它仍然报告错误说“消息有效负载是类型:字符串”。
  • 尝试使用更简单的 xml 和 xpath 表达式,我的 xpath 技能不是很好,可能我没有考虑命名空间。
  • 尝试使用 来避免消息负载类型:字符串
  • 嗨 Julio,这就是我正在使用的,我在我的流程中使用 。但它仍然有同样的错误。我的流程包括:TTP、CXF、DOM TO XML、SetPayload、DataMapper 和 Database
【解决方案2】:

将 Http Inbound 与配置了 wsdl 详细信息的 SOAP 连接器一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多