【问题标题】:How to store the external webservice response value in a flow variable using Enricher如何使用 Enricher 将外部 Web 服务响应值存储在流变量中
【发布时间】:2015-07-21 16:50:40
【问题描述】:

我遇到了一个小问题 .. 有 My Mule 流程 .. 我将 SOAP 有效负载从 Message Enricher 组件传递给外部 Web 服务,想法是从外部 Web 服务响应中获取特定属性值并将该值存储在流变量中...... 现在,外部 Web 服务响应的响应如下:-

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <getDesignationResponse xmlns="http://services.test.getDesignation.com/schema/MainData/V1">
         <DesignationCodeResult>Junior Developer</DesignationCodeResult>
         <Code>Success</Code>
      </getDesignationResponse>
   </soap:Body>
</soap:Envelope> 

现在这是我的流程:-

 <flow name="testFlow1" doc:name="testFlow1">
   <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="test" doc:name="HTTP"/>

<set-payload value="<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://services.test.getDesignation.com/schema/MainData/V1"><soapenv:Header/><soapenv:Body><v1:getDesignationRequest><v1:DesignationCode>jd</v1:DesignationCode></v1:getDesignationRequest></soapenv:Body></soapenv:Envelope>" doc:name="Set Payload"/>

  <enricher source="#[xpath://getDesignationResponse/DesignationCodeResult]" target="#[variable:myVal]" doc:name="Message Enricher">
  <processor-chain doc:name="Processor Chain">
    <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://localhost:8090/designation" responseTimeout="100000" doc:name="HTTP" />
    <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>

     <logger message="Value from response #[xpath://getDesignationResponse/DesignationCodeResult]" level="INFO" doc:name="Logger"/>
   </processor-chain>           
</enricher> 

</flow>

现在您可以看到我正在尝试将值 #[xpath://getDesignationResponse/DesignationCodeResult] 存储到流变量 myVal ..

现在我得到以下异常:-

Exception stack is:
1. Expression Evaluator "header" with expression "invocation:myVal" returned null but a value was required. (org.mule.api.expression.RequiredValueException)
  org.mule.expression.ExpressionUtils:235 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/expression/RequiredValueException.html)
2. Expression Evaluator "header" with expression "invocation:myVal" returned null but a value was required. (org.mule.api.expression.RequiredValueException). Message payload is of type: String (org.mule.api.MessagingException)
  org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.expression.RequiredValueException: Expression Evaluator "header" with expression "invocation:myVal" returned null but a value was required.
    at org.mule.expression.ExpressionUtils.getPropertyInternal(ExpressionUtils.java:235)
    at org.mule.expression.ExpressionUtils.getProperty(ExpressionUtils.java:85)
    at org.mule.expression.ExpressionUtils.getProperty(ExpressionUtils.java:72)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

但我已经在 Enricher &lt;logger message="Value from response #[xpath://getDesignationResponse/DesignationCodeResult]" level="INFO" doc:name="Logger"/&gt; 中放置了一个记录器,我在那里得到了价值.. 但是我无法使用 Enricher 在流变量中分配提取的值... 请帮忙..

【问题讨论】:

  • 如果删除记录器会怎样?我想知道您是否正在使用记录器使用 HTTP 响应输入流,从而使其对浓缩器不可用...
  • 尝试从 Enricher 中删除记录器......我得到了同样的异常

标签: mule mule-studio


【解决方案1】:

从帖子中可以看出,被发布的响应有一个命名空间

xmlns="http://services.test.getDesignation.com/schema/MainData/V1"

命名空间 http://services.test.getDesignation.com/schema/MainData/V1 应该在你的 mule config XML 命名空间管理器中注册。

那么xpath就可以正确识别响应中的xml元素了

根据评论更新答案:

在您的流程中尝试以下修改后的浓缩器部分。

<enricher source="#[payload]" target="#[variable:myVal]" doc:name="Message Enricher">
    <processor-chain doc:name="Processor Chain">
    <http:outbound-endpoint exchange-pattern="request-response" method="POST" address="http://localhost:8090/designation" responseTimeout="100000" doc:name="HTTP" />
    <mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
    <logger message="Value from response #[xpath://getDesignationResponse/DesignationCodeResult]" level="INFO" doc:name="Logger"/>
     <set-payload value="#[xpath://getDesignationResponse/DesignationCodeResult]" />
              </processor-chain>           
</enricher>

希望这会有所帮助。

【讨论】:

  • 是的,它已经完成了,我在 Enricher 内的记录器中获取 DesignationCodeResult 的值.. 但无法在浓缩器的流变量中设置它
  • 你是对的 .. 命名空间有小问题 .. 需要为此修改命名空间和 XPATH .. 无论如何 +1 为你的好建议.. 大卫的建议对我有用.. 但很多非常感谢您的帮助..
  • 总是乐于提供帮助:)
【解决方案2】:

首先添加命名空间支持:

<mulexml:namespace-manager>
    <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
    <mulexml:namespace prefix="vertu" uri="http://services.vertu.getDesignation.com/schema/MainData/V1"/>
</mulexml:namespace-manager>

然后停止使用旧的不推荐使用的表达式语法并改用 MEL:

<enricher
    source="#[xpath('//vertu:getDesignationResponse/vertu:DesignationCodeResult/text()').text]"
    target="#[flowVars['myVal']]">

此外,如果您想保留记录器,请在入站 HTTP 端点之后添加一个 object-to-string-transformer,以便将响应输入流反序列化为一个可多次读取的 String

【讨论】:

    【解决方案3】:

    所以,根据大卫的建议,解决方案是纠正命名空间问题,工作代码是:-

    <mulexml:namespace-manager>
        <mulexml:namespace prefix="soapenv" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
        <mulexml:namespace prefix="vertu" uri="http://services.vertu.getDesignation.com/schema/MainData/V1"/>
    </mulexml:namespace-manager>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      相关资源
      最近更新 更多