Configuring a Router with Annotations
当使用 @Router
为方法添加注释时,该方法可以返回 MessageChannel
或 String
类型。在后一种情况下,端点解析信道名称,其方式与它解析默认输出信道的方式相同。此外,该方法可以返回单个值或集合。如果返回集合,则应答消息将被发送到多个信道。总而言之,以下方法签名都 valid:
When using @Router
to annotate a method, the method may return either a MessageChannel
or a String
type.
In the latter case, the endpoint resolves the channel name as it does for the default output channel.
Additionally, the method may return either a single value or a collection.
If a collection is returned, the reply message is sent to multiple channels.
To summarize, the following method signatures are all valid:
@Router
public MessageChannel route(Message message) {...}
@Router
public List<MessageChannel> route(Message message) {...}
@Router
public String route(Foo payload) {...}
@Router
public List<String> route(Foo payload) {...}
除了基于有效负载的路由,还可根据消息标头中作为属性或特征可用的元数据对消息进行路由。在这种情况下,使用 @Router
注释的方法可能包含使用 @Header
注释的参数,该参数映射到标头值,如下例所示,并在 Annotation Support 中进行了说明:
In addition to payload-based routing, a message may be routed based on metadata available within the message header as either a property or an attribute.
In this case, a method annotated with @Router
may include a parameter annotated with @Header
, which is mapped to a header value as the following example shows and documented in Annotation Support:
@Router
public List<String> route(@Header("orderStatus") OrderStatus status)
对于 XML 消息的路由(包括 XPath 支持),请参阅 XML Support - Dealing with XML Payloads。 |
For routing of XML-based Messages, including XPath support, see XML Support - Dealing with XML Payloads. |
有关路由器配置的更多信息,请参阅 Java DSL 章节中的 Message Routers。
See also Message Routers in the Java DSL chapter for more information about router configuration.