Message Conversion for Annotated Methods
在调用监听器之前,管道中有两个转换步骤。第一步使用 MessageConverter
将传入的 Spring AMQP Message
转换为 Spring 消息传递 Message
。当调用目标方法时,消息有效负载会根据需要转换为方法参数类型。
There are two conversion steps in the pipeline before invoking the listener.
The first step uses a MessageConverter
to convert the incoming Spring AMQP Message
to a Spring-messaging Message
.
When the target method is invoked, the message payload is converted, if necessary, to the method parameter type.
针对第一步的默认 MessageConverter`是一个 Spring AMQP `SimpleMessageConverter
,用于处理到`String`和 java.io.Serializable`对象的转换。所有其他对象仍为 `byte[]
。在以下讨论中,我们称之为 “message converter”。
The default MessageConverter
for the first step is a Spring AMQP SimpleMessageConverter
that handles conversion to
String
and java.io.Serializable
objects.
All others remain as a byte[]
.
In the following discussion, we call this the “message converter”.
第二步的默认转换器是 GenericMessageConverter
,它委派给转换服务(DefaultFormattingConversionService
的实例)。在下面的讨论中,我们称之为“方法参数转换器
”。
The default converter for the second step is a GenericMessageConverter
, which delegates to a conversion service
(an instance of DefaultFormattingConversionService
).
In the following discussion, we call this the “method argument converter”.
若要更改消息转换器,你可以将其作为一个属性添加到容器工厂 bean。以下示例展示了如何执行此操作:
To change the message converter, you can add it as a property to the container factory bean. The following example shows how to do so:
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
...
factory.setMessageConverter(new Jackson2JsonMessageConverter());
...
return factory;
}
这配置了一个 Jackson2 转换器,该转换器希望标头信息存在,以指导转换。
This configures a Jackson2 converter that expects header information to be present to guide the conversion.
你还可以使用 ContentTypeDelegatingMessageConverter
,它可以处理不同内容类型的转换。
You can also use a ContentTypeDelegatingMessageConverter
, which can handle conversion of different content types.
从 2.3 版开始,您可以在 messageConverter
属性中指定 bean 名称来覆盖工厂转换器。
Starting with version 2.3, you can override the factory converter by specifying a bean name in the messageConverter
property.
@Bean
public Jackson2JsonMessageConverter jsonConverter() {
return new Jackson2JsonMessageConverter();
}
@RabbitListener(..., messageConverter = "jsonConverter")
public void listen(String in) {
...
}
这样无需声明不同的容器工厂即可更改转换器。
This avoids having to declare a different container factory just to change the converter.
大多数情况下,无需自定义方法参数转换器,除非您要使用自定义 ConversionService
。
In most cases, it is not necessary to customize the method argument converter unless, for example, you want to use
a custom ConversionService
.
在 1.6 之前的版本中,必须在消息头中提供 JSON 转换类型信息,或需要自定义 ClassMapper
。从 1.6 版本开始,如果没有类型信息头,则可从目标方法参数推断类型。
In versions prior to 1.6, the type information to convert the JSON had to be provided in message headers, or a
custom ClassMapper
was required.
Starting with version 1.6, if there are no type information headers, the type can be inferred from the target
method arguments.
此类型推断仅适用于方法级别的 |
This type inference works only for |
有关详细信息,请参见 xref:amqp/message-converters.adoc#json-message-converter[Jackson2JsonMessageConverter
。
See Jackson2JsonMessageConverter
for more information.
如果要自定义方法参数转换器,可以按照以下步骤进行:
If you wish to customize the method argument converter, you can do so as follows:
@Configuration
@EnableRabbit
public class AppConfig implements RabbitListenerConfigurer {
...
@Bean
public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
factory.setMessageConverter(new GenericMessageConverter(myConversionService()));
return factory;
}
@Bean
public DefaultConversionService myConversionService() {
DefaultConversionService conv = new DefaultConversionService();
conv.addConverter(mySpecialConverter());
return conv;
}
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
}
...
}
对于多方法侦听器(请参阅 Multi-method Listeners),方法选择基于消息的有效负载 after the message conversion。仅在方法被选中后才调用方法参数转换器。
For multi-method listeners (see Multi-method Listeners), the method selection is based on the payload of the message after the message conversion. The method argument converter is called only after the method has been selected.