Routing XML Messages with XPath
类似于基于 SpEL 的路由器,Spring Integration 提供了基于 XPath 表达式的路由消息的支持,这允许你创建一个具有输入通道但没有输出通道的消息端点。相反,一个或多个输出通道将动态确定。以下示例展示了如何创建这样的路由器:
Similar to SpEL-based routers, Spring Integration provides support for routing messages based on XPath expressions, which lets you create a message endpoint with an input channel but no output channel. Instead, one or more output channels are determined dynamically. The following example shows how to create such a router:
<int-xml:xpath-router id="orderTypeRouter" input-channel="orderChannel">
<int-xml:xpath-expression expression="/order/type"/>
</int-xml:xpath-router>
有关路由器之间常见的属性的概述,请参阅 Common Router Parameters。 |
For an overview of attributes that are common among Routers, see Common Router Parameters. |
在内部,XPath 表达式作为 NODESET
类型进行评估,并转换为表示通道名称的 List<String>
。通常,此类列表包含一个通道名称。但是,基于 XPath 表达式的结果,如果 XPath 路由器返回多个值,则它还可以采用收件人列表路由器的特征。在这种情况下,List<String>
包含多个通道名称。因此,邮件将发送到列表中的所有通道。
Internally, XPath expressions are evaluated as type NODESET
and converted to a List<String>
that represents channel names.
Typically, such a list contains a single channel name.
However, based on the results of an XPath Expression, the XPath router can also take on the characteristics of a recipient list router if the XPath expression returns more than one value.
In that case, the List<String>
contains more than one channel name.
Consequently, messages are sent to all the channels in the list.
因此,假设传递给以下路由器配置的 XML 文件包含许多表示通道名称的 responder
子元素,则邮件将发送到所有这些通道:
Thus, assuming that the XML file passed to the following router configuration contains many responder
sub-elements that represent channel names, the message is sent to all of those channels:
<!-- route the order to all responders-->
<int-xml:xpath-router id="responderRouter" input-channel="orderChannel">
<int-xml:xpath-expression expression="/request/responders"/>
</int-xml:xpath-router>
如果返回的值不直接表示通道名称,则可以指定其他映射参数以将这些返回的值映射到实际的通道名称。例如,如果 /request/responders
表达式产生两个值(responderA
和 responderB
),但你不希望将应答器名称与通道名称耦合,则可以提供其他映射配置,如下所示:
If the returned values do not represent the channel names directly, you can specify additional mapping parameters to map those returned values to actual channel names.
For example if the /request/responders
expression results in two values (responderA
and responderB
), but you do not want to couple the responder names to channel names, you can provide additional mapping configuration, such as the following:
<!-- route the order to all responders-->
<int-xml:xpath-router id="responderRouter" input-channel="orderChannel">
<int-xml:xpath-expression expression="/request/responders"/>
<int-xml:mapping value="responderA" channel="channelA"/>
<int-xml:mapping value="responderB" channel="channelB"/>
</int-xml:xpath-router>
如前所述,XPath 表达式的默认评估类型是 NODESET
,它被转换为通道名称的 List<String>
,它可以处理单个通道场景以及多个通道场景。
As already mentioned, the default evaluation type for XPath expressions is NODESET
, which is converted to a List<String>
of channel names, which handles single channel scenarios as well as multiple channel scenarios.
尽管如此,某些 XPath 表达式可能从一开始就评估为 String
类型。例如,考虑以下 XPath 表达式:
Nonetheless, certain XPath expressions may evaluate as type String
from the very beginning.
Consider, for example, the following XPath Expression:
name(./node())
此表达式返回根节点的名称。如果使用默认评估类型 NODESET
,则会导致异常。
This expression returns the name of the root node.
If the default evaluation type NODESET
is being used, it results in an exception.
对于这些场景,你可以使用 evaluate-as-string
属性,它允许你管理评估类型。默认情况下,它是 FALSE
。但是,如果你将其设置为 TRUE
,则会使用 String
评估类型。
For these scenarios, you can use the evaluate-as-string
attribute, which lets you manage the evaluation type.
It is FALSE
by default.
However, if you set it to TRUE
, the String
evaluation type is used.
XPath 1.0 指定了 4 个数据类型: XPath 1.0 specifies 4 data types:
当 XPath 路由器使用可选的 When the XPath Router evaluates expressions by using the optional 有关更多信息,请参阅: For further information, see: |
比如,如果我们希望基于根节点的名称进行路由,我们可以使用以下配置:
For example, if we want to route based on the name of the root node, we can use the following configuration:
<int-xml:xpath-router id="xpathRouterAsString"
input-channel="xpathStringChannel"
evaluate-as-string="true">
<int-xml:xpath-expression expression="name(./node())"/>
</int-xml:xpath-router>
XML Payload Converter
对于 XPath 路由器,还可以指定在 XPath 评估之前转换有效负载时要使用的转换器。因此,XPath 路由器支持 XmlPayloadConverter
策略的自定义实现,并在 XML 中配置 xpath-router
元素时,可以使用 converter
属性提供对这种实现的引用。
For XPath Routers, you can also specify the Converter to use when converting payloads prior to XPath evaluation.
As such, the XPath Router supports custom implementations of the XmlPayloadConverter
strategy, and when configuring an xpath-router
element in XML, a reference to such an implementation may be provided via the converter
attribute.
如果没有明确提供此引用,则使用 DefaultXmlPayloadConverter
。在大多数情况下,它都应该足够,因为它可以从 Node、Document、Source、File 和 String 类型的有效负载转换。如果你需要超越该默认实现的能力,那么在大多数情况下,上游转换器通常是更好的选择,而不是在此处提供对该策略的自定义实现的引用。
If this reference is not explicitly provided, the DefaultXmlPayloadConverter
is used.
It should be sufficient in most cases, since it can convert from Node, Document, Source, File, and String typed payloads.
If you need to extend beyond the capabilities of that default implementation, then an upstream Transformer is generally a better option in most cases, rather than providing a reference to a custom implementation of this strategy here.