Apache camel xpath 条件路由
Apache camel xpath conditional routing
我的应用程序上下文中有一条骆驼路线定义为
<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 条件中更改什么?
谢谢
在 "when" 条件下,尝试使用 "simple" 而不是 "xpath"。替换这些词。
-><简单>
将 <xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello/toWhom = 'normal'</xpath>
更改为 <simple>${body} contains 'normal'</simple>
时有效
但我想让它与 xpath 一起工作..
尝试为消费者端点设置不同的 dataFormat
参数。尝试 CXF_MESSAGE
:
<from uri="cxf:bean:cdcPocProxyWebServiceStartPoint?dataFormat=CXF_MESSAGE" />
此外,尝试将您的 <xpath ... />
更改为使用以下格式:
<xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello[toWhom='normal']</xpath>
你的第一个例子似乎是正确的。问题是您如何发送消息,导致 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
我试图设置大部分日志,但我做不到。编辑说 "Your post appears to contain code that is not properly formatted as code"。可悲的是我不知道该怎么做。
希望我的 post 能帮到你。
我的应用程序上下文中有一条骆驼路线定义为
<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 条件中更改什么?
谢谢
在 "when" 条件下,尝试使用 "simple" 而不是 "xpath"。替换这些词。
将 <xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello/toWhom = 'normal'</xpath>
更改为 <simple>${body} contains 'normal'</simple>
但我想让它与 xpath 一起工作..
尝试为消费者端点设置不同的 dataFormat
参数。尝试 CXF_MESSAGE
:
<from uri="cxf:bean:cdcPocProxyWebServiceStartPoint?dataFormat=CXF_MESSAGE" />
此外,尝试将您的 <xpath ... />
更改为使用以下格式:
<xpath>//soapenv:Envelope/soapenv:Body/hel:sayHello[toWhom='normal']</xpath>
你的第一个例子似乎是正确的。问题是您如何发送消息,导致 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
我试图设置大部分日志,但我做不到。编辑说 "Your post appears to contain code that is not properly formatted as code"。可悲的是我不知道该怎么做。 希望我的 post 能帮到你。