This version is still in development and is not considered stable yet. For the latest stable version, please use Spring AMQP 3.2.0! |
Adding a Custom HandlerMethodArgumentResolver
to @RabbitListener
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)
);
}
}
);
}
}