如何使用Enricher将外部Web服务响应值存储在流变量中

时间:2014-09-28 17:55:04

标签: mule mule-studio

我遇到了一个小问题..有我的骡子流...... 我将SOAP负载从Message Enricher组件传递到外部Web服务,Idea是从外部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 <logger message="Value from response #[xpath://getDesignationResponse/DesignationCodeResult]" level="INFO" doc:name="Logger"/>中加入了一个记录器,我在那里获得了价值。 但是我无法使用Enricher在流变量中分配提取的值... 请帮忙..

3 个答案:

答案 0 :(得分:1)

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

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

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

然后xpath将能够正确识别响应中的xml元素

根据评论更新了答案:

尝试以下修改过的Richher部分。

<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>

希望这有帮助。

答案 1 :(得分:0)

首先添加名称空间支持:

<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,这可以多次读取。

答案 2 :(得分:0)

因此,根据David的建议,解决方案是纠正命名空间问题,工作代码是: -

<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>