如何使用 richHeaders 方法使用 java dsl 获取 xml 元素的 xpath

发布于 2025-01-11 04:55:27 字数 594 浏览 3 评论 0原文

我正在尝试在 Spring 集成中使用 java dsl 获取 xml 元素的 xpth。我试图将元素名称放入丰富标题方法中,但它似乎不起作用。这是代码:

return IntegrationFlows.from(Http.inboundGateway("/person")
                        .requestMapping(m -> m.methods(HttpMethod.POST)
                                .consumes("application/xml")
                                .produces("application/json")
                        )
                        )
                .enrichHeaders( h -> h.header("bsn","/BG:prsLa01/BG:body/BG:object/BG:bsn"))
                .transform(new XmlToJsonTransformer())
                .get();

i am trying to pick up an xpth of an xml element using java dsl in spring integration. I am trying to put the element name in the enrichHeaders method but it doesn't seem to be working. This is the code:

return IntegrationFlows.from(Http.inboundGateway("/person")
                        .requestMapping(m -> m.methods(HttpMethod.POST)
                                .consumes("application/xml")
                                .produces("application/json")
                        )
                        )
                .enrichHeaders( h -> h.header("bsn","/BG:prsLa01/BG:body/BG:object/BG:bsn"))
                .transform(new XmlToJsonTransformer())
                .get();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹雨成花 2025-01-18 04:55:27

支持 xPath:

https://docs.spring.io/spring-integration/docs/5.2.5.RELEASE/reference/html/xml.html#xpath-spel-function

xpath SpEL功能

Spring Integration从3.0版本开始,提供了内置的#xpath
SpEL 函数,调用 XPathUtils.evaluate(... ) static
方法。该方法委托给一个
org.springframework.xml.xpath.XPathExpression。以下列表
显示一些使用示例:

<transformer expression="#xpath(payload, '/name')"/>

<filter expression="#xpath(payload, headers.xpath, 'boolean')"/>

<splitter expression="#xpath(payload, '//book', 'document_list')"/>

<router expression="#xpath(payload, '/person/@age', 'number')">
    <mapping channel="output1" value="16"/>
    <mapping channel="output2" value="45"/>
</router>

#xpath() 还支持第三个可选参数用于转换
XPath 评估的结果。它可以是字符串之一
常量(字符串、布尔值、数字、节点、node_list 和 document_list)
或 org.springframework.xml.xpath.NodeMapper 实例。默认情况下,
#xpath SpEL 函数返回 XPath 的字符串表示形式
评价。

要启用#xpath SpEL功能,您可以添加
spring-integration-xml.jar 到类路径。您无需声明任何
来自 Spring Integration XML 命名空间的组件。了解更多
信息,请参阅“`Spring 表达式语言 (SpEL)。

它可能如下所示:

.enrichHeaders(h -> 
             h.headerExpression("bsn", "#xpath(payload, '/BG:body/BG:object/BG:bsn')"))

另一种方法可能是 XPathExpressionEvaluatingHeaderValueMessageProcessor

https://docs.spring.io/spring-integration/api/org/springframework/integration/xml/transformer/support/XPathExpressionEvaluatingHeaderValueMessageProcessor.html

使用示例:

        @Bean
        public IntegrationFlow xpathHeaderEnricherFlow() {
            return IntegrationFlows.from("xpathHeaderEnricherInput")
                    .enrichHeaders(
                            s -> s.header("one",
                                    new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementOne"))
                                    .header("two",
                                            new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementTwo"))
                                    .headerChannelsToString("12345")
                                    .messageProcessor(m -> s.header("foo", "bar")),
                            c -> c.autoStartup(false).id("xpathHeaderEnricher")
                    )
                    .get();
        }

There is support for xPath:

https://docs.spring.io/spring-integration/docs/5.2.5.RELEASE/reference/html/xml.html#xpath-spel-function

xpath SpEL Function

Spring Integration, since version 3.0, provides the built-in #xpath
SpEL function, which invokes the XPathUtils.evaluate(…​) static
method. This method delegates to an
org.springframework.xml.xpath.XPathExpression. The following listing
shows some usage examples:

<transformer expression="#xpath(payload, '/name')"/>

<filter expression="#xpath(payload, headers.xpath, 'boolean')"/>

<splitter expression="#xpath(payload, '//book', 'document_list')"/>

<router expression="#xpath(payload, '/person/@age', 'number')">
    <mapping channel="output1" value="16"/>
    <mapping channel="output2" value="45"/>
</router>

The #xpath() also supports a third optional parameter for converting
the result of the XPath evaluation. It can be one of the String
constants (string, boolean, number, node, node_list and document_list)
or an org.springframework.xml.xpath.NodeMapper instance. By default,
the #xpath SpEL function returns a String representation of the XPath
evaluation.

To enable the #xpath SpEL function, you can add the
spring-integration-xml.jar to the classpath. You need no declare any
components from the Spring Integration XML Namespace. For more
information, see "`Spring Expression Language (SpEL).

It could look like this:

.enrichHeaders(h -> 
             h.headerExpression("bsn", "#xpath(payload, '/BG:body/BG:object/BG:bsn')"))

Another way could be to XPathExpressionEvaluatingHeaderValueMessageProcessor:

https://docs.spring.io/spring-integration/api/org/springframework/integration/xml/transformer/support/XPathExpressionEvaluatingHeaderValueMessageProcessor.html

Example of Usage:

        @Bean
        public IntegrationFlow xpathHeaderEnricherFlow() {
            return IntegrationFlows.from("xpathHeaderEnricherInput")
                    .enrichHeaders(
                            s -> s.header("one",
                                    new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementOne"))
                                    .header("two",
                                            new XPathExpressionEvaluatingHeaderValueMessageProcessor("/root/elementTwo"))
                                    .headerChannelsToString("12345")
                                    .messageProcessor(m -> s.header("foo", "bar")),
                            c -> c.autoStartup(false).id("xpathHeaderEnricher")
                    )
                    .get();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文