【问题标题】:Spring IntegrationFlow Filter and TransformSpring IntegrationFlow过滤器和转换
【发布时间】:2020-03-14 14:21:52
【问题描述】:

我有一些 XML 正在尝试通过集成流。以下是我正在执行的步骤:

  • 如果消息包含 XML 节点,则过滤掉消息:“filterMe”并且类型 =“filterType” - 如果是,我不想处理它。
  • 将其从 XML 转换为 Kotlin 类,并在消息中添加一些标头。

我找不到正确过滤 2 个 XML 属性上的消息的方法。非常感谢任何帮助。

这是一些 XML 示例

<?xml version="1.0" encoding="UTF-8"?>
<Response type="filterType">//Filter on this
   <event>
      <header>
         <type>Test</type>
      </header>
      <body>
         <filterMe>filter me out</filterMe> //Filter this too
      </body>
   </event>
</Response>

集成流程

        return IntegrationFlows
                .from(inputChannel)
                .filter("filterMe"::contains)
                .transform { payload: String ->
                    val jsonMessage = XML.toJSONObject(payload).toString()

【问题讨论】:

    标签: java spring spring-boot kotlin spring-integration


    【解决方案1】:

    考虑倾斜XPath

    使用 filter() 中的 Spring Integration,您可以这样做:

    .filter(payload -> 
                    XPathUtils.evaluate(payload, "/Response/@type = \"filterType\" and //filterMe", 
                                        XPathUtils.BOOLEAN))
    

    注意:您应该在类路径中有spring-integration-xml 才能访问该XPathUtils

    【讨论】:

    • 非常感谢,这很好用。我也学到了很多关于 x 路径的知识,所以也谢谢你。
    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多