Adding a Custom HandlerMethodArgumentResolver
to @RabbitListener
从版本 2.3.7 开始,你可以添加自己的 HandlerMethodArgumentResolver
并解析自定义方法参数。你所需要做的就是实施 RabbitListenerConfigurer
并使用 RabbitListenerEndpointRegistrar
类中的 setCustomMethodArgumentResolvers()
方法。
Starting with version 2.3.7 you are able to add your own HandlerMethodArgumentResolver
and resolve custom method parameters.
All you need is to implement RabbitListenerConfigurer
and use method setCustomMethodArgumentResolvers()
from class RabbitListenerEndpointRegistrar
.
@Configuration
class CustomRabbitConfig implements RabbitListenerConfigurer {
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setCustomMethodArgumentResolvers(
new HandlerMethodArgumentResolver() {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return CustomMethodArgument.class.isAssignableFrom(parameter.getParameterType());
}
@Override
public Object resolveArgument(MethodParameter parameter, org.springframework.messaging.Message<?> message) {
return new CustomMethodArgument(
(String) message.getPayload(),
message.getHeaders().get("customHeader", String.class)
);
}
}
);
}
}