I am using Spring Integration. I get a string (payload) like below:

<Element>
<Sub-Element>5</Sub-Element>
</Element>

I need to test if above string starts with <Element><Sub-Element> which is actually <Element>\r\n <Sub-Element>.

<int:recipient-list-router id="customRouter" input-channel="routingChannel">
    <int:recipient channel="channel1" selector-expression="payload.startsWith('&lt;Element&gt;&lt;Sub-Element&gt;')"/>
    <int:recipient channel="channel2" selector-expression="!payload.startsWith('&lt;Element&gt;&lt;Sub-Element&gt;')"/>
</int:recipient-list-router>

Ideally the first router should pass the test but in this case its failing. Can anyone help me finding out what is the SpEL equivalent of \r \n etc ?

有帮助吗?

解决方案 2

Thanks Gary. So the working list-recipient-router looks like

Either

<recipient selector-expression="payload matches '(?s)&lt;Element&gt;(\s*)&lt;Sub&gt;(.*)'" channel="channel1"/>
<recipient selector-expression="!(payload matches '(?s)&lt;Element&gt;(\s*)&lt;Sub&gt;(.*)')" channel="channel2"/>

Or

<recipient selector-expression="payload matches '(?s)&lt;Element&gt;(.*)&lt;Sub&gt;(.*)'" channel="channel1"/>
    <recipient selector-expression="!(payload matches '(?s)&lt;Element&gt;(.*)&lt;Sub&gt;(.*)')" channel="channel2"/>

May keep captures () or may not. Both works.

其他提示

SpEL doesn't have escapes for those, but you can use regular expressions to do the selection...

<recipient selector-expression="payload matches '&lt;Element&gt;\r\n&lt;Sub-Element&gt;.*'" channel="channel1"/>
<recipient selector-expression="!(payload matches '&lt;Element&gt;\r\n&lt;Sub-Element&gt;.*')" channel="channel2"/>

If you are not familiar with regex, the .* at the end matches anything (hence this regex is the equivalent of startsWith()).

EDIT:

While this will work, I feel I should point out that relying on specific values in insignificant white space in XML documents is brittle - if the client changes to use, say \n instead, or even no whitespace, your application will break. You should consider using something like an <int-xml:xpath-router/> instead.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top