【问题标题】:Apache camel xpath conditional routingApache camel xpath 条件路由
【发布时间】:2018-02-14 18:56:36
【问题描述】:

我的应用程序上下文中有一条骆驼路线定义为

<camelContext xmlns="http://camel.apache.org/schema/spring"
    trace="true" id="camel">

    <route id="wsProxyRedirection">
        <from uri="cxf:bean:cdcPocProxyWebServiceStartPoint" />
        <choice>
            <when>
                <xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello/toWhom = 'normal'</xpath>
                <to uri="jms:queue:normalQueue" />
            </when>
            <when>
                <xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello/toWhom = 'urgent'</xpath>
                <to uri="jms:queue:urgentQueue" />
            </when>
            <when>
                <xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello/toWhom = 'veryUrgent'</xpath>
                <to uri="jms:queue:veryUrgentQueue" />
            </when>
        </choice>
    </route>
</camelContext>

我发送的请求是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://cxf.apache.org/wsse/handler/helloworld">
   <soapenv:Header/>
   <soapenv:Body>
      <hel:sayHello>
         <toWhom>normal</toWhom>
      </hel:sayHello>
   </soapenv:Body>
</soapenv:Envelope>

但我得到的回应是:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Error during type conversion from type: java.lang.String to the required type: org.w3c.dom.Document with value test due org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.</faultstring>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

这只是一个简单的选择 POC,但我无法让它工作......我必须在我的 xpath 条件中更改什么?

谢谢

【问题讨论】:

    标签: java xpath soap apache-camel cxf


    【解决方案1】:

    在“when”条件下,尝试使用“simple”而不是“xpath”。替换这些词。

    ->

    【讨论】:

      【解决方案2】:

      &lt;xpath&gt;//soapenv:Envelope/soapenv:Body/hel:sayHello/toWhom = 'normal'&lt;/xpath&gt; 更改为&lt;simple&gt;${body} contains 'normal'&lt;/simple&gt; 时有效

      但我想让它与 xpath 一起使用..

      【讨论】:

        【解决方案3】:

        尝试为消费者端点设置不同的dataFormat 参数。试试CXF_MESSAGE:

        <from uri="cxf:bean:cdcPocProxyWebServiceStartPoint?dataFormat=CXF_MESSAGE" />
        

        另外,请尝试将您的 &lt;xpath ... /&gt; 更改为使用以下格式:

        &lt;xpath&gt;//soapenv:Envelope/soapenv:Body/hel:sayHello[toWhom='normal']&lt;/xpath&gt;

        【讨论】:

        • 如果将dataFormat 设置为MESSAGE,有什么变化吗?
        • 消息没有被消费,出现超时错误
        • 尝试将?exchangePattern=InOnly参数添加到jms端点。 (例如&lt;to uri="jms:queue:normalQueue?exchangePattern=InOnly" /&gt;.
        • 同样的问题,我尝试使用PAYLOAD 数据格式(因为不推荐使用MESSAGE)。我得到了响应(不再超时),但是我无法做出条件选择(未提取该字段)。
        • 如果是PAYLOAD,试着把xpath改成/hel:sayHello[toWhom='normal'],因为body的内容就是soap:Body! (camel.apache.org/cxf.html#CXF-Thedescriptionsofthedataformats)
        【解决方案4】:

        您的第一个示例似乎是正确的。问题是您如何发送消息,导致 cxf 接收的数据对 xml 无效,因此您收到 SAXParseException。 当然,您不应该使用“${simple} contains”,因为您使用的是 xml,而 apache camel 提供了很多工具来使用它。 我根据您的要求创建测试示例。

        Cxf 端点:

        <camelcxf:cxfEndpoint
                id="cdcPocProxyWebServiceStartPoint"
                wsdlURL="etc/Test.WSDL"
                address="/services/request">
            <camelcxf:properties>
                <entry key="dataFormat" value="PAYLOAD"/>
            </camelcxf:properties>
        </camelcxf:cxfEndpoint>
        

        当您使用有效负载数据格式时,您将完全使用 soap:Body 内容,并且您不需要在 xpath 中使用 SOAP 标记。

        路线:

        <camelContext id="incomingRequestsProcessing" xmlns="http://camel.apache.org/schema/spring">
            <route id="wsProxyRedirection" xmlns:hel="http://cxf.apache.org/wsse/handler/helloworld">
                <from uri="cxf:bean:cdcPocProxyWebServiceStartPoint?loggingFeatureEnabled=true"/>
                <choice>
                    <when>
                        <xpath>//hel:sayHello/toWhom = 'normal'</xpath>
                        <log message="Receive average hello status"/>
                    </when>
                    <when>
                        <xpath>//hel:sayHello/toWhom = 'urgent'</xpath>
                        <log message="Receive urgent hello status"/>
                    </when>
                    <when>
                        <xpath>//hel:sayHello/toWhom = 'veryUrgent'</xpath>
                        <log message="Receive very urgent hello status"/>
                    </when>
                </choice>
            </route>
        </camelContext>
        

        我使用 SoapUI 进行测试。发送请求的结果是:

        2017-10-13 17:19:37,745 | INFO  | tp1048175215-590 | wsProxyRedirection               | 178 - org.apache.camel.camel-core - 2.16.3 | Receive average hello status
        

        我试图设置大部分日志,但我做不到。编辑器说“您的帖子似乎包含未正确格式化为代码的代码”。可悲的是我不知道该怎么做。 希望我的帖子能帮到你。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-29
          • 2018-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多