此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1spring-doc.cadn.net.cn

出站消息转换

Spring AMQP 1.4 引入了ContentTypeDelegatingMessageConverter,其中实际转换器是根据 在传入内容类型消息属性上。 这可以由入站端点使用。spring-doc.cadn.net.cn

从 Spring Integration 4.3 版本开始,您可以使用ContentTypeDelegatingMessageConverter在出站端点上,使用contentType标头,指定使用哪个转换器。spring-doc.cadn.net.cn

以下示例配置ContentTypeDelegatingMessageConverter,默认转换器是SimpleMessageConverter(处理 Java 序列化和纯文本),以及 JSON 转换器:spring-doc.cadn.net.cn

<amqp:outbound-channel-adapter id="withContentTypeConverter" channel="ctRequestChannel"
                               exchange-name="someExchange"
                               routing-key="someKey"
                               amqp-template="amqpTemplateContentTypeConverter" />

<int:channel id="ctRequestChannel"/>

<rabbit:template id="amqpTemplateContentTypeConverter"
        connection-factory="connectionFactory" message-converter="ctConverter" />

<bean id="ctConverter"
        class="o.s.amqp.support.converter.ContentTypeDelegatingMessageConverter">
    <property name="delegates">
        <map>
            <entry key="application/json">
                <bean class="o.s.amqp.support.converter.Jackson2JsonMessageConverter" />
            </entry>
        </map>
    </property>
</bean>

将消息发送到ctRequestChannel使用contentTypeheader 设置为application/json导致选择 JSON 转换器。spring-doc.cadn.net.cn

这适用于出站通道适配器和网关。spring-doc.cadn.net.cn

从 5.0 版开始,添加到MessageProperties的出站消息永远不会被映射的标头覆盖(默认情况下)。 以前,仅当消息转换器是ContentTypeDelegatingMessageConverter(在这种情况下,首先映射标头,以便可以选择正确的转换器)。 对于其他转换器,例如SimpleMessageConverter,映射的标头覆盖了转换器添加的任何标头。 当出站邮件有一些剩余时,这会导致问题contentType标头(可能来自入站通道适配器)和正确的出站contentType被错误地覆盖了。 解决方法是在将邮件发送到出站终结点之前使用标头筛选器删除标头。spring-doc.cadn.net.cn

但是,在某些情况下,需要以前的行为——例如,当String有效负载,则SimpleMessageConverter不知道内容,并将contentTypemessage 属性设置为text/plain但您的应用程序希望将其重写为application/json通过将contentType发送到出站终结点的邮件的标头。 这ObjectToJsonTransformer正是这样做的(默认情况下)。spring-doc.cadn.net.cn

现在有一个名为headersMappedLast在出站通道适配器和网关(以及 AMQP 支持的通道上)。 将此设置为true恢复覆盖转换器添加的属性的行为。spring-doc.cadn.net.cn

从 5.1.9 版本开始,类似的replyHeadersMappedLastAmqpInboundGateway当我们生成回复并想要覆盖转换器填充的标头时。 有关更多信息,请参阅其 JavaDocs。spring-doc.cadn.net.cn