xpath-router不使用xpath-expression路由消息

时间:2017-07-18 17:14:16

标签: java spring xpath spring-integration

我使用带有xpath-expression

的Spring Integration XML路由器
<int:chain input-channel="routerInputChannel">
    <int-xml:xpath-router default-output-channel="routerOutputChannel">
        <int-xml:xpath-expression expression="//actor[1]/text()"/>
        <int-xml:mapping value="Christian Bale" channel="routerBaleChannel"/>
    </int-xml:xpath-router>
</int:chain>

并期望此XML字符串作为SI消息有效负载

<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
    <actors>
        <actor id="1">Christian Bale</actor>
        <actor id="2">Liam Neeson</actor>
        <actor id="3">Michael Caine</actor>
    </actors>
    <foo:singers>
        <foo:singer id="4">Tom Waits</foo:singer>
        <foo:singer id="5">B.B. King</foo:singer>
        <foo:singer id="6">Ray Charles</foo:singer>
    </foo:singers>
</root>

将发送给routerBaleChannel但由于某种原因它无法正常工作。

我在XPath Tester / Evaluator尝试了邮件和XPath表达式,正如预期的那样,我已经获得了Christian Bale字符串。

我调试了org.springframework.integration.xml.router.XPathRouter方法getChannelKeys并在Jaxp13XPathExpressionFactory方法evaluate中看到了一些问题:执行后出于未知原因

NodeList nodes = (NodeList) evaluate(node, XPathConstants.NODESET);

nodes.getLength()返回空列表。

那么我的XPath路由器或XPath表达式配置有什么问题?

1 个答案:

答案 0 :(得分:0)

适合我:

<int:chain input-channel="inputChannel">
    <int-xml:xpath-router>
        <int-xml:xpath-expression expression="//actor[1]/text()"/>
        <int-xml:mapping value="Christian Bale" channel="routerBaleChannel"/>
    </int-xml:xpath-router>
</int:chain>

<int:channel id="routerBaleChannel">
    <int:queue/>
</int:channel>

...

@Test
public void testSO45173221() {
    ClassPathXmlApplicationContext ac =
            new ClassPathXmlApplicationContext("XPathRouterTests-context.xml", this.getClass());
    MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);
    PollableChannel channelBale = ac.getBean("routerBaleChannel", PollableChannel.class);
    GenericMessage<String> message =
            new GenericMessage<>("<root xmlns:foo=\"http://www.foo.org/\" xmlns:bar=\"http://www.bar.org\">\n" +
            "    <actors>\n" +
            "        <actor id=\"1\">Christian Bale</actor>\n" +
            "        <actor id=\"2\">Liam Neeson</actor>\n" +
            "        <actor id=\"3\">Michael Caine</actor>\n" +
            "    </actors>\n" +
            "    <foo:singers>\n" +
            "        <foo:singer id=\"4\">Tom Waits</foo:singer>\n" +
            "        <foo:singer id=\"5\">B.B. King</foo:singer>\n" +
            "        <foo:singer id=\"6\">Ray Charles</foo:singer>\n" +
            "    </foo:singers>\n" +
            "</root>");
    inputChannel.send(message);
    Message<?> result = channelBale.receive(0);
    assertNotNull(result);
    assertEquals(message.getPayload(), result.getPayload());
    ac.close();
}

不确定你的情况发生了什么......

虽然我使用的是Java 8 FYI。